Not able to get data when NEXT is clicked in Java Selenium

Here is my code:

package working;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Test {

    private static WebDriver driver;

    public static void main(String[] args) throws Exception {
        openBrowser();
        System.out.println(scrape());
        closeBrowser();
    }

    private static String table = "(//table[contains(@class,'datatable')])[1]";
    private static String tr = table + "/tbody/tr";
    private final static String next = table + "/preceding::a[text()='Next'][1]";
    private final static String yesGotIt = "//body/div[contains(concat(" ",normalize-space(@class)," ")," gdpr-banner ")]/div/div[contains(concat(" ",normalize-space(@class)," ")," gdpr-button-wrapper ")]/button";

    private static String scrape() throws Exception {
        WebElement table_element = driver.findElement(By.xpath(table));
        List<WebElement> tr_collection = table_element.findElements(By.xpath(tr));

        int count = 1;
        String rows = "";
        driver.findElement(By.xpath(yesGotIt)).click();
        while(count <= 3) {
            for (WebElement trElement : tr_collection) {
                List<WebElement> td_collection = trElement.findElements(By.xpath("td"));
                for (WebElement tdElement : td_collection) {
                    rows += tdElement.getText() + "t";
                }
                rows = rows + "n";
            }

            System.out.println(rows);
            WebDriverWait wait = new WebDriverWait(driver,30);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(next)));
            driver.findElement(By.xpath(next)).click();
            count += driver.findElements(By.xpath(next)).size();
            System.out.println(count);
        }
        return rows;
    }

    private static void openBrowser() {
        System.setProperty("webdriver.chrome.driver",
                "E:\eclipse-workspace\src\working\selenium\driver\chromedriver.exe");
        System.setProperty("webdriver.chrome.silentOutput", "true");
        driver = new ChromeDriver();
        driver.get("https://www.iata.org/en/publications/directories/code-search/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    private static void closeBrowser() {
        driver.quit();
    }
}

without while loop and next the code is working fine but when I run the aforementioned code its printing the first page table data 3 times repeatedly and throws the following error:

Mar 20, 2021 11:21:47 AM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS “Fly Armenia Airways” Limited Liability Company Armenia VF 914 “NOVAIR” Limited Liability Company Armenia NG 206 21 Air LLC UNITED STATES OF AMERICA 2I* 681 40-Mile Air, Ltd. UNITED STATES OF AMERICA Q5* 748 Air Services Kenya H4* 874 874

2 “Fly Armenia Airways” Limited Liability Company Armenia VF 914
“NOVAIR” Limited Liability Company Armenia NG 206 21 Air LLC UNITED STATES OF AMERICA 2I* 681 40-Mile Air, Ltd. UNITED STATES OF AMERICA Q5* 748 Air Services Kenya H4* 874 874 “Fly Armenia Airways” Limited Liability Company Armenia VF 914 “NOVAIR” Limited Liability Company Armenia NG 206 21 Air LLC UNITED STATES OF AMERICA 2I* 681 40-Mile Air, Ltd. UNITED STATES OF AMERICA Q5*
748 Air Services Kenya H4* 874 874

3 Exception in thread “main” org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document (Session info: chrome=89.0.4389.90) (Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.18363 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/stale_element_reference.html Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:25:53’ System info: host: ‘DESKTOP-832OS5B’, ip: ‘10.122.250.5’, os.name: ‘Windows 10’, os.arch: ‘amd64’, os.version: ‘10.0’, java.version: ‘15.0.2’ Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.42.591088 (7b2b2dca23cca0…, userDataDir: C:UsersWindowsAppDataLo…}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:59261}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 89.0.4389.90, webStorageEnabled: true} Session ID: 5064d7cddc475396a421aa6d1639fb5c at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166) at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40) at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80) at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285) at org.openqa.selenium.remote.RemoteWebElement.getText(RemoteWebElement.java:166) at working.Test.scrape(Test.java:41) at working.Test.main(Test.java:21)

How can I print each page table data by clicking NEXT button?

Best regards

Answer

  • Refresh page after clicking Next button each time.
  • Declare table_element, tr_collection inside while.
  • Declare scrape() with void return and just call scrape() without print.
  • Also did some other changes in code.
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Test 
{
    private static WebDriver driver;

    public static void main(String[] args) throws Exception 
    {
        openBrowser();
        //System.out.println(scrape());
        scrape();
        closeBrowser();
    }

    private static String table = "(//table[contains(@class,'datatable')])[1]";
    private static String tr = table + "/tbody/tr";
    private final static String next = table + "/preceding::a[text()='Next'][1]";
    private final static String yesGotIt = "//body/div[contains(concat(" ",normalize-space(@class)," ")," gdpr-banner ")]/div/div[contains(concat(" ",normalize-space(@class)," ")," gdpr-button-wrapper ")]/button";

    private static void scrape() throws Exception 
    {
        // WebElement table_element = driver.findElement(By.xpath(table));
        // List<WebElement> tr_collection = table_element.findElements(By.xpath(tr));

        int count = 1;
        // String rows = "";
        driver.findElement(By.xpath(yesGotIt)).click();
        while (count <= 3) {
            //Declare rows here - previous page's data will not repeatedly print
            String rows = "";

            System.out.println(count);

            // Declare WebElement, List here
            WebElement table_element = driver.findElement(By.xpath(table));
            List<WebElement> tr_collection = table_element.findElements(By.xpath(tr));

            for (WebElement trElement : tr_collection) 
            {
                List<WebElement> td_collection = trElement.findElements(By.xpath("td"));
                for (WebElement tdElement : td_collection) 
                {
                    rows += tdElement.getText() + "t";
                }
                rows = rows + "n";
            }

            System.out.println(rows);
            WebDriverWait wait = new WebDriverWait(driver, 30);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(next)));
            driver.findElement(By.xpath(next)).click();
            count += driver.findElements(By.xpath(next)).size();
            //System.out.println(count);

            //refresh page to ignore StaleElementReferenceException
            driver.navigate().refresh();
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(next)));
        }
    }

    private static void openBrowser() 
    {
        System.setProperty("webdriver.chrome.driver",
            "E:\eclipse-workspace\src\working\selenium\driver\chromedriver.exe");
        System.setProperty("webdriver.chrome.silentOutput", "true");
        driver = new ChromeDriver();
        driver.get("https://www.iata.org/en/publications/directories/code-search/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    private static void closeBrowser() 
    {
        driver.quit();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *