Java Selenium printing table rows repeatedly for first page only

Here is my code:

public class Test 
{        
    private static String table         = "(//table[contains(@class,'datatable')])[last()]";
    private static String tr            = table+ "/tbody/tr";
    private final static String next    = table+"/preceding::a[text()='Next'][last()]";
    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 
    {
        int count = 1;
        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();            

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

}

There are two tables of same class in one page. The aforementioned code works fine when I test first table by using xpath as "(//table[contains(@class,'datatable')])[0]" but when I try to test second table by changing its xpath as "(//table[contains(@class,'datatable')])[last()]"; then its scraping first page data repeatedly for Next pages despite displaying other pages data

Best regards

Answer

As visibility is focused on first table, last table’s page change is not observable. To focus on actual table’s next button JavascriptExecutor is used and updated the whole code.

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

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
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 table = "(//table[contains(@class,'datatable')])[last()]";
    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)));
            
            //scroll to next button
            JavascriptExecutor je = (JavascriptExecutor) driver;
            WebElement we = driver.findElement(By.xpath(next));
            je.executeScript("arguments[0].scrollIntoView(true);", we);
            we.click();
            
            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 *