Back to All Scenarios
Scenario 16 of 100
Synchronization
Advanced
Syncing Scrolls & Waits on Infinite Scroll Pages
πScenario Overview
Syncing Scrolls & Waits on Infinite Scroll Pages
Key Takeaways & Cheat Sheet
- βCalculate document scroll height before and after scrolling actions
- βScroll sequentially using window.scrollTo() via JavaScript Executor
- βWait explicitly for new items or content count to increase after each scroll
- βImplement a maximum scroll retry limit to prevent infinite loops
Short Direct Answer
Infinite scroll pages load content dynamically as the user scrolls down the viewport. To automate this, you must scroll programmatically using JavaScript, wait for the DOM to update (such as tracking the count of loaded items or checking if the page height increased), and scroll again until either the target element is visible or the bottom of the feed is reached.
β οΈ Senior Warning (Red Flag)
Never trigger an infinite scroll loop without a defined maximum count or page limit. If the feed is truly infinite, your script will run out of memory or exceed the CI/CD pipeline timeout.
π‘ STAR Deep Dive Explanation & Pro Tip
Always implement a safety break. If new items fail to render after a scroll, break the loop immediately rather than waiting for the timeout.
SeleniumAutomation.java
Selenium 4 + Java// β
Infinite Scroll Automation Strategy
public void scrollToLoadItems(By itemLocator, int targetCount, int maxScrolls) {
JavascriptExecutor js = (JavascriptExecutor) driver;
int scrollAttempts = 0;
while (scrollAttempts < maxScrolls) {
int currentSize = driver.findElements(itemLocator).size();
if (currentSize >= targetCount) break;
// Scroll to the bottom of the page
js.executeScript("window.scrollTo(0, document.body.scrollHeight);");
// Wait explicitly for new elements to load into DOM
try {
new WebDriverWait(driver, Duration.ofSeconds(5)).until(
d -> d.findElements(itemLocator).size() > currentSize
);
} catch (TimeoutException e) {
System.out.println("No new elements loaded. Reached bottom.");
break;
}
scrollAttempts++;
}
}