Selenium 如何检索元素的属性?
嗨,我有一个html代码:Selenium 如何检索元素的属性?,selenium,Selenium,嗨,我有一个html代码: <img style="border-width:0px;" alt="graph" src="abc.ashx?meter=1203&start=20131007000000&end=20131028000000" id="ctl00_phBody_imgChart"> 请任何人在同一方面提供帮助 谢谢 Sudhansu您的开始和结束在属性src中,因此您需要获取src并获取所需信息,thorugh regexp,试试这
<img style="border-width:0px;" alt="graph" src="abc.ashx?meter=1203&start=20131007000000&end=20131028000000" id="ctl00_phBody_imgChart">
请任何人在同一方面提供帮助
谢谢
Sudhansu您的开始和结束在属性src中,因此您需要获取src并获取所需信息,thorugh regexp,试试这个(Java示例):
String attribute = driver.findElement(By.id("ctl00_phBody_imgChart")).getAttribute("src");
//atribute = abc.ashx?meter=1203&start=20131007000000&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);
}
String属性=driver.findelelement(By.id(“ctl00\u phBody\u imgChart”)).getAttribute(“src”);
//阿曲布他=abc.ashx?流量计=1203&;开始=20131007000000&;完=20131028000000
把你的开始串起来;
把绳子系在你的头上;
Pattern patternStart=Pattern.compile(“(?请添加您正在处理的示例html代码,以便为您提供准确的帮助。您得到的是什么错误?……“GetAttribute”中缺少“e”,我想这是一个输入错误。。这是我正在使用的代码,即使查找元素的格式不正确,它应该是“driver.findElement(By.id)(“ctl00\u phBody\u imgChart”))“…请编辑您的问题并重写您试图编译的代码..抱歉,格式是:driver.findElement(By.id(“ctl00”\u phBody\u imgChart”)).getAttribute(“start”);这足够吗?
String attribute = driver.findElement(By.id("ctl00_phBody_imgChart")).getAttribute("src");
//atribute = abc.ashx?meter=1203&start=20131007000000&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);
}