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();
 }
asked Feb 20, 2020 at 15:46

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();
answered Feb 20, 2020 at 15:53
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. Commented Feb 20, 2020 at 16:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.