1

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

这是我的代码:

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)
4

1 回答 1

1

该错误表明它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");
}
于 2018-01-19T07:16:21.893 回答