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