即使元素存在(使用Firebug / FirePath验证),Selenium WebDriver也会抛出NoSuchElementException

时间:2014-11-05 14:30:41

标签: java selenium xpath nosuchelementexception

我在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;">&lt;&lt;</a>&nbsp;<a title="Föregående" class="PagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl05','')" style="display:inline;">&lt;</a>&nbsp;<a class="PagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl06','')" style="display:inline;">...</a>&nbsp;<a class="PagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl07','')">161</a>&nbsp;<a class="PagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl08','')">162</a>&nbsp;<a class="PagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl09','')" style="display:none;">...</a>&nbsp;<a title="Nästa" class="PagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl10','')" style="display:none;">&gt;</a>&nbsp;<a title="Sista" class="PagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl11','')" style="display:none;">&gt;&gt;</a>&nbsp;<br><a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl12','')">A</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl13','')">B</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl14','')">C</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl15','')">D</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl16','')">E</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl17','')">F</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl18','')">G</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl19','')">H</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl20','')">I</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl21','')">J</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl22','')">K</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl23','')">L</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl24','')">M</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl25','')">N</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl26','')">O</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl27','')">P</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl28','')">Q</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl29','')">R</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl30','')">S</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl31','')">T</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl32','')">U</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl33','')">V</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl34','')">W</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl35','')">X</a>&nbsp;Y&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl36','')">Z</a>&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl37','')">Å</a>&nbsp;Ä&nbsp;<a href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl38','')">Ö</a>&nbsp;&nbsp;<a class="SelectedPagerItem" href="javascript:__doPostBack('ctl01$cphContent$Quickrank1$ctl00$ctl03$ctl01$ctl39','')">Alla</a>

0 个答案:

没有答案