πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
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)));
    }
}