Back to All Scenarios
Scenario 47 of 100
Data Handling
Intermediate
Automating and Verifying Table Pagination
βοΈScenario Overview
Automating and Verifying Table Pagination
Key Takeaways & Cheat Sheet
- βLocate and track navigation elements (Next/Prev page links)
- βLoop click Next page controls until the button becomes disabled
- βExtract and accumulate table row records on every page
- βCompare total accumulated records against summary footers
Short Direct Answer
To automate pagination, implement a loop that extracts data from the current page and clicks the "Next" button. Continue this process until the "Next" button has a "disabled" attribute or CSS class indicating the last page has been reached.
β οΈ Senior Warning (Red Flag)
Never loop without checking if the Next button is disabled. Doing so will trigger an infinite loop or cause exceptions when attempting to click non-interactive elements.
π‘ STAR Deep Dive Explanation & Pro Tip
Always verify staleness of a dynamic element from the old page before scraping the next one. This ensures the table has fully reloaded and you do not scrape duplicate data.
SeleniumAutomation.java
Selenium 4 + Java// β
Paginated Data Scraper Strategy
List<String> userList = new ArrayList<>();
boolean hasNextPage = true;
while (hasNextPage) {
// 1. Scrap cell data from active page
List<WebElement> names = driver.findElements(By.xpath("//table/tbody/tr/td[1]"));
for (WebElement name : names) {
userList.add(name.getText());
}
// 2. Locate and inspect Next Page control
WebElement nextBtn = driver.findElement(By.className("pagination-next"));
String classValue = nextBtn.getAttribute("class");
if (classValue.contains("disabled")) {
hasNextPage = false; // Reached last page
} else {
nextBtn.click();
// Wait for table DOM loader to clear
wait.until(ExpectedConditions.stalenessOf(names.get(0)));
}
}