我在Selenium WebDriver中使用此代码来提取非Ajax网页上某个元素的值。
public int getNumberOfPages(WebDriver driver) {
try {
webDriverBackedSelenium.click(linkLastPage);
1 webDriverBackedSelenium.waitForPageToLoad(normalTimeout);
2 new WebDriverWait(driver, normalTimeoutS).until(ExpectedConditions
.presenceOfElementLocated(By.xpath(linkFirstPage)));
3 return Integer.parseInt(driver.findElement(By.xpath(pathToNumberOfPages))
.getText());
} catch (NumberFormatException e){
e.printStackTrace();
webDriverBackedSelenium.click(linkPreviousPage);
webDriverBackedSelenium.waitForPageToLoad(normalTimeout);
} catch (org.openqa.selenium.NoSuchElementException e){
e.printStackTrace();
}
try {
4 return Integer.parseInt(driver.findElement(By.xpath(pathToNumberOfPages))
.getText());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
如果我在(1)处有一个断点,打开Firebug并插入XPath,找到该元素,但是当我运行它时,我在3和4都得到NoSuchElementException(如果我在那里设置断点,再次使用FirePath,元素按预期发现。)
如果等待,例如,上面的1和2,一次一个,同时两个,我尝试了不同的类型,总是有相同的结果。
超时设置为30秒/ 30 000毫秒。
我读过两个类似的问题
但那里的答案不适用于我的情况(因为我已经验证了元素与FirePath一起)。
bl ** dy元素是那么为什么Selenium没有找到它?
的XPath
//tr[@class='rgPager']//a[@class='PagerItem'] [@title='Första']
HTML
<a title="Första" class="PagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl04','')" style="display:inline;"><<</a> <a title="Föregående" class="PagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl05','')" style="display:inline;"><</a> <a class="PagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl06','')" style="display:inline;">...</a> <a class="PagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl07','')">161</a> <a class="PagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl08','')">162</a> <a class="PagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl09','')" style="display:none;">...</a> <a title="Nästa" class="PagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl10','')" style="display:none;">></a> <a title="Sista" class="PagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl11','')" style="display:none;">>></a> <br><a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl12','')">A</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl13','')">B</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl14','')">C</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl15','')">D</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl16','')">E</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl17','')">F</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl18','')">G</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl19','')">H</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl20','')">I</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl21','')">J</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl22','')">K</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl23','')">L</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl24','')">M</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl25','')">N</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl26','')">O</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl27','')">P</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl28','')">Q</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl29','')">R</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl30','')">S</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl31','')">T</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl32','')">U</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl33','')">V</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl34','')">W</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl35','')">X</a> Y <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl36','')">Z</a> <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl37','')">Å</a> Ä <a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl38','')">Ö</a> <a class="SelectedPagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl39','')">Alla</a>