java - 无法解决循环中的 "Stale element reference exception"

标签 java exception selenium-webdriver staleelementreferenceexception

我正在尝试单击循环内的分页。

这是我的代码:

WebElement pagination = d.findElement(By.xpath("/html/body/div[9]/div[1]/div[3]/div/div/div[2]/ul"));
List < WebElement > allPaginations = pagination.findElements(By.tagName("a"));
WebElement title = d.findElement(By.linkText(">"));
System.out.println(allPaginations.size());
if (allPaginations.size() > 0)
{
    System.out.println("Pagination exists");
    for (int i = 0; i < allPaginations.size(); i++)
    {
        allPaginations = pagination.findElements(By.tagName("a"));
        Thread.sleep(3000);
        allPaginations.get(i).click();
        d.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
        List < WebElement > ngo_Names = d.findElements(By.xpath("/html/body/div[9]/div[1]/div[3]/div/div/div[2]/table/tbody/tr/td[2]"));
        System.out.println(ngo_Names.size());
        //System.out.println(i);
    }
}
else
{
    System.out.println("Pagination doesn't exists");
}

但是在尝试第二次单击时,我遇到了无法修复的异常。有关如何解决此问题的任何建议。提前致谢

编辑:1

这是我遇到的异常:

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: The element reference of <ul class="pagination"> stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.5.2', revision: '10229a9020', time: '2017-08-21T17:54:21.164Z'
System info: host: 'SAURABH', ip: '192.168.0.205', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\Saurabh\AppData\Local\Temp\rust_mozprofile.m5FGXXXy6WIb, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, moz:headless=false, platform=ANY, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=57.0.4, platformVersion=10.0, moz:processID=4752, browserName=firefox, javascriptEnabled=true, platformName=windows_nt, moz:webdriverClick=false}]
Session ID: d3be2955-cc46-4884-aff2-b54ca73ccc37
*** Element info: {Using=tag name, value=a}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:185)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:120)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:641)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:275)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:194)
at org.openqa.selenium.remote.RemoteWebElement.findElementsByTagName(RemoteWebElement.java:271)
at org.openqa.selenium.By$ByTagName.findElements(By.java:327)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:170)
at ngpdarpan.Pagination.DataPull(Pagination.java:74)
at ngpdarpan.Pagination.main(Pagination.java:35)

最佳答案

该错误表明 pagination 不再位于 DOM 中。解决办法很简单,只需在循环中再次查找即可。

WebElement pagination = d.findElement(By.xpath("/html/body/div[9]/div[1]/div[3]/div/div/div[2]/ul"));
List < WebElement > allPaginations = pagination.findElements(By.tagName("a"));
WebElement title = d.findElement(By.linkText(">"));
System.out.println(allPaginations.size());
if (allPaginations.size() > 0)
{
    System.out.println("Pagination exists");
    for (int i = 0; i < allPaginations.size(); i++)
    {
        pagination = d.findElement(By.xpath("/html/body/div[9]/div[1]/div[3]/div/div/div[2]/ul"));
        allPaginations = pagination.findElements(By.tagName("a"));
        Thread.sleep(3000);
        allPaginations.get(i).click();
        d.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
        List < WebElement > ngo_Names = d.findElements(By.xpath("/html/body/div[9]/div[1]/div[3]/div/div/div[2]/table/tbody/tr/td[2]"));
        System.out.println(ngo_Names.size());
        //System.out.println(i);
    }
}
else
{
    System.out.println("Pagination doesn't exists");
}

关于java - 无法解决循环中的 "Stale element reference exception",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48334094/

相关文章:

java - 当获取设置了超时的 Future 结果时,哪里抛出了异常?

java - ReSTLet 在 HTTPS BindException 上与 Jetty 一起运行

java - 使用chromedriver直接下载PDF文件

java - 无法将 gson 转换为我的对象类型

java - Eclipse 速度极慢,持续阻塞在 NonEssentialElementsFilter.hasFilteredChildren

java - 在 Activity 之间来回传递数据

c# - Selenium WebDriver : how to count number of DIV elements in a list

java - 为什么Java中字节范围是-128到127?

java - 套接字异常 : Connection reset after reconnect

java - 如果加载时间太长,如何让 selenium 重新加载所需的 url