1

I am trying to click on pagination inside loop.

Here is my code:

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");
}

But while trying clicking for second time, I'm getting exception which I'm not able to fix. Any suggestions on how to solve this. Thanks in advance

Edit:1

Here is the which exception I'm getting:

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

1 Answer 1

1

The error shows that pagination was not in the DOM anymore. The solution is very simple, you just need to find it again in the loop.

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");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.