0

I'm trying to handle elements from multiple pages via the same loop after click next button like on website:

        driver.get("https://www.scimagojr.com/journalrank.php?country=UA&page=1");
        List<Journal> journalList = new ArrayList<>();

        WebElement tableId = driver.findElement(By.tagName("table"));
        List<WebElement> trElements = tableId.findElements(By.xpath("./tbody/tr"));

        //...

        for (int i = 0; i < count / 50; i++) {
            for (int id = 1; id <= trElements.size(); id++) {
                for (WebElement element : trElements) {
                    String title = element.findElement(By.xpath("./td[2]/a")).getText();
                    String country = "Ukraine";
                    journalList.add(new Journal(id, title, country));
                    id++;
                }
            }
            WebElement element = driver.findElementByXPath("(//div[@class='pagination_buttons']/a)[2]");
            element.click();
        }

The problem is this loop is going through the first page only one time. When I click next button on line:

WebElement element = driver.findElementByXPath("(//div[@class='pagination_buttons']/a)[2]");

looks like variable: trElements, which I'm getting, isn't updating and I can't go again through same loop on the next page.

Can someone show me an example how to handle multiple pages via the same loop dynamically? No matter how many pages website will have.

But I'd prefer to handle pages while the next button is clickable.

Updated:

        driver.get("https://www.scimagojr.com/journalrank.php?country=UA&page=1");
        List<Journal> journalList = new ArrayList<>();

        //...

        for (int i = 0; i < count / 50; i++) {
            WebElement tableId = driver.findElement(By.tagName("table"));
            List<WebElement> trElements = tableId.findElements(By.xpath("./tbody/tr"));

            for (int id = 1; id <= trElements.size(); id++) {
                WebElement element = trElements.get(id);
                String title = element.findElement(By.xpath("./td[2]/a")).getText();
                String country = "Ukraine";
                journalList.add(new Journal(id, title, country));

            }
            WebElement element = driver.findElementByXPath("(//div[@class='pagination_buttons']/a)[2]");
            element.click();
        }

1 Answer 1

2

After you do element.click(); your DOM gets rebuilt so after that you have your trElements stayed filled with stale elements.

I would recommend to bring your List<WebElement> trElements = tableId.findElements(By.xpath("./tbody/tr")); into the i-loop. There is also not much value in your inner loop hence you can do it with in your id-loop using the approach like:

String title = trElements.get(id-1).findElement(By.xpath("./td[2]/a")).getText();
1
  • Thanks for pointing out about rebuilding DOM, Alexey R. I'll try your approach and, if it helps me, I'll mark your answer as acceptable.
    – invzbl3
    Feb 20, 2020 at 16:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.