Scrolling

Scroll with Actions

WebElement footer = driver.findElement(By.id("footer"));
new Actions(driver).scrollToElement(footer).perform();      // bring the element into view
new Actions(driver).scrollByAmount(0, 800).perform();       // scroll down 800 px

Selenium usually scrolls automatically before a click. You need explicit scrolling for infinite scroll pages (more products load when you reach the bottom) and lazy-loaded images.

Infinite Scroll

int previous = 0;
while (true) {
    List<WebElement> cards = driver.findElements(By.cssSelector(".product-card"));
    if (cards.size() == previous || cards.size() >= 100) break;
    previous = cards.size();
    new Actions(driver).scrollToElement(cards.get(cards.size() - 1)).perform();
    wait.until(d -> d.findElements(By.cssSelector(".product-card")).size() > previous);
}
System.out.println("Loaded cards: " + previous);

Example

Output:

Run InfiniteScrollTest.main()

Example output

Loaded cards: 96

Note: The lambda uses previous, so in real code copy it to a final local variable before the wait. Scrolling can also be done with JavaScriptExecutor (next module).