2013-10-27 73 views
0

您好我有一個HTML代碼:如何檢索元素的屬性?

<img style="border-width:0px;" alt="graph" src="abc.ashx?meter=1203&amp;start=20131007000000&amp;end=20131028000000" id="ctl00_phBody_imgChart"> 

我要檢索的開始結束參數。我試着用下面的代碼,但沒有爲我工作。

driver.findElement.id("ctl00_phBody_imgChart").getAttribut("start") 

請任何人都幫助。

感謝,

Sudhansu

+1

請添加示例html代碼您的工作內容如此幫助您c一個準確。 – abrasadera

+0

wat error is u getting?.....「e」is missing from「getAttribut」,I guess that is a typo .. – Amith

+0

graph這是我正在使用的代碼 –

回答

1

開始在屬性SRC,所以你需要讓src和得到你需要的信息,thorugh正則表達式,試試這個( Java示例)

String attribute = driver.findElement(By.id("ctl00_phBody_imgChart")).getAttribute("src"); 
//atribute = abc.ashx?meter=1203&amp;start=20131007000000&amp;end=20131028000000 
String yourStart; 
String yourEnd; 

Pattern patternStart = Pattern.compile("(?<=start=)(.*?)(?=\;)"); 
Pattern patternEnd = Pattern.compile("(?<=end=)(.*?).*"); 

Matcher matcherStart = patternStart.matcher(attribute); 
Matcher matcherEnd = patternEnd.matcher(attribute); 

if (matcherStart.find()) { 
     yourStart = matcherStart.group(1); 
} 
if (matcherEnd.find()) { 
     yourEnd = matcherEnd.group(1); 
}