How can I scroll a web page using selenium webdriver in python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Selenium WebDriver in Python provides several ways to scroll a web page: executing JavaScript with driver.execute_script(), using keyboard actions via the ActionChains class, or scrolling to specific elements with scrollIntoView(). The most common approach is driver.execute_script("window.scrollTo(0, document.body.scrollHeight)") to scroll to the bottom of the page. For infinite-scroll pages, use a loop that scrolls and waits for new content to load.
Setup
Scroll to Bottom of Page
window.scrollTo(x, y) scrolls to an absolute position. window.scrollBy(x, y) scrolls relative to the current position. document.body.scrollHeight is the total height of the page content.
Scroll to a Specific Element
scrollIntoView(true) aligns the element to the top of the viewport. scrollIntoView({block: 'center'}) centers it vertically.
Scroll Using Keyboard Keys
Infinite Scroll Handling
The loop compares scrollHeight before and after each scroll. When the height stops increasing, all content has loaded.
Scroll Inside a Scrollable Container
When content is inside a scrollable div (with overflow: auto or overflow: scroll), you must scroll that specific element, not the window.
Smooth Scroll with Custom Speed
Wait for Element After Scrolling
Use WebDriverWait instead of time.sleep() for more reliable waits. It polls for the element and returns as soon as it appears, up to the timeout.
Common Pitfalls
- Using
time.sleep()instead of explicit waits: Hard-coded sleeps are unreliable — content may load faster or slower than expected. UseWebDriverWaitwithexpected_conditionsfor robust waiting after scrolling. - Scrolling the window when content is in a scrollable div: If the target content is inside a container with its own scrollbar,
window.scrollTo()has no effect on it. Find the container element and set itsscrollTopproperty instead. - Infinite scroll loop without a max scroll limit: If the page generates content endlessly (e.g., social media feeds), a scroll loop without
max_scrollsruns forever. Always include a maximum iteration count. - Not waiting for dynamic content after scrolling: Lazy-loaded images and AJAX content need time to render after scrolling. Scrolling and immediately reading the DOM may miss content that has not loaded yet.
- Forgetting that
scrollIntoViewdoes not wait for visibility:scrollIntoView()scrolls the page but does not guarantee the element is visible or interactable. UseWebDriverWaitwithelement_to_be_clickablebefore interacting with the element.
Summary
- Use
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")to scroll to the bottom - Use
scrollIntoView()orActionChains.move_to_element()to scroll to a specific element - Handle infinite-scroll pages with a loop that compares
scrollHeightbefore and after each scroll - Scroll inside containers by setting their
scrollTopproperty, notwindow.scrollTo() - Use
WebDriverWaitinstead oftime.sleep()for reliable content detection after scrolling - Set
max_scrollslimits to prevent infinite loops on endlessly loading pages
Related reading
- How can I see function arguments in IPython Notebook Server 3?
- How can I see the entire HTTP request that's being sent by my Python application?
- How can I see the raw SQL queries Django is running?
- How can I selectively escape percent in Python strings?
- How can I serve my django website from multiple machines, that is how can I make it distributed?
- How can I set a default value for a field in a Django model?
- How can I set up a virtual environment for Python in Visual Studio Code?
- How can I sort a list of dictionaries by a value of the dictionary in Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.