</>

Technology

Selenium

Difficulty

Intermediate

Interview Question

How do you handle infinite scrolling pages in Selenium?

Answer

Handling Infinite Scrolling in Selenium

Infinite scroll pages (like LinkedIn, Twitter, Instagram) load more content as you scroll down instead of using pagination.

Method 1 — Scroll to Page Bottom (Loop)

Java
JavascriptExecutor js = (JavascriptExecutor) driver;
long lastHeight = (long) js.executeScript("return document.body.scrollHeight");

while (true) {
    // Scroll to bottom
    js.executeScript("window.scrollTo(0, document.body.scrollHeight);");

    // Wait for new content to load
    Thread.sleep(2000);

    long newHeight = (long) js.executeScript("return document.body.scrollHeight");

    if (newHeight == lastHeight) {
        // No new content loaded — we''ve reached the end
        break;
    }
    lastHeight = newHeight;
}

System.out.println("Reached end of infinite scroll");

Method 2 — Scroll Until Target Count Reached

Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
JavascriptExecutor js = (JavascriptExecutor) driver;

int targetCount = 50;
int previousCount = 0;

while (true) {
    List<WebElement> items = driver.findElements(By.cssSelector(".feed-item"));

    if (items.size() >= targetCount) break;

    if (items.size() == previousCount) {
        System.out.println("No new items loaded. Stopping.");
        break;
    }

    previousCount = items.size();

    // Scroll last item into view
    js.executeScript("arguments[0].scrollIntoView(true);", items.get(items.size() - 1));

    // Wait for new items to load
    wait.until(d -> d.findElements(By.cssSelector(".feed-item")).size() > previousCount);
}

Method 3 — Scroll N Times (When Count Unknown)

Java
JavascriptExecutor js = (JavascriptExecutor) driver;
int scrollTimes = 10;

for (int i = 0; i < scrollTimes; i++) {
    js.executeScript("window.scrollBy(0, 1000);"); // scroll 1000px down
    Thread.sleep(1500); // wait for content
}

// Collect all loaded items
List<WebElement> allItems = driver.findElements(By.cssSelector(".product-card"));
System.out.println("Total items loaded: " + allItems.size());

Method 4 — Scroll to Specific Element (Load Until Found)

Java
String targetText = "Selenium Framework";
JavascriptExecutor js = (JavascriptExecutor) driver;
boolean found = false;

for (int attempt = 0; attempt < 20 && !found; attempt++) {
    List<WebElement> items = driver.findElements(By.cssSelector(".item-title"));

    for (WebElement item : items) {
        if (item.getText().contains(targetText)) {
            found = true;
            item.click();
            break;
        }
    }

    if (!found) {
        js.executeScript("window.scrollTo(0, document.body.scrollHeight);");
        Thread.sleep(1500);
    }
}

Assert.assertTrue(found, "Target item '" + targetText + "' not found after scrolling");

Extract All Data After Full Scroll

Java
// First scroll through entire page
scrollToBottom(driver);

// Then collect all loaded data
List<WebElement> allProducts = driver.findElements(By.cssSelector(".product"));
List<String> productNames = allProducts.stream()
    .map(WebElement::getText)
    .collect(Collectors.toList());

System.out.println("Total products: " + productNames.size());

Follow AutomateQA

Related Topics