How to Scroll Page in Selenium Python: Syntax and Examples
To scroll a page in Selenium Python, use the
execute_script method with JavaScript commands like window.scrollBy(x, y) or window.scrollTo(x, y). This lets you scroll vertically or horizontally by pixels or to specific positions on the page.Syntax
Use Selenium's execute_script method to run JavaScript for scrolling. Common commands include:
window.scrollBy(x, y): Scrolls the page byxpixels horizontally andypixels vertically from the current position.window.scrollTo(x, y): Scrolls the page to the exact coordinates(x, y).window.scrollTo(0, document.body.scrollHeight): Scrolls to the bottom of the page.
python
driver.execute_script("window.scrollBy(0, 500)") # Scroll down 500 pixels driver.execute_script("window.scrollTo(0, 0)") # Scroll to top of the page driver.execute_script("window.scrollTo(0, document.body.scrollHeight)") # Scroll to bottom
Example
This example opens a webpage, scrolls down 1000 pixels, waits 2 seconds, then scrolls to the bottom of the page.
python
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options import time options = Options() options.add_argument('--headless') # Run browser in headless mode service = Service() # Adjust path if needed driver = webdriver.Chrome(service=service, options=options) try: driver.get('https://example.com') # Scroll down 1000 pixels driver.execute_script("window.scrollBy(0, 1000)") time.sleep(2) # Wait to see the scroll effect # Scroll to bottom of the page driver.execute_script("window.scrollTo(0, document.body.scrollHeight)") time.sleep(2) finally: driver.quit()
Output
No visible output; browser scrolls page as scripted.
Common Pitfalls
- Not waiting after scroll: The page may need time to load new content after scrolling, so use
time.sleep()or explicit waits. - Wrong scroll coordinates: Using negative or too large values can cause no effect or errors.
- Scrolling before page load: Ensure the page is fully loaded before scrolling.
- Using deprecated methods: Avoid older Selenium scroll methods; use
execute_scriptwith JavaScript instead.
python
from selenium import webdriver import time # Initialize driver driver = webdriver.Chrome() driver.get('https://example.com') # Wrong: scrolling before page load # driver.execute_script("window.scrollBy(0, 1000)") # May fail if page not loaded # Right: wait for page load then scroll import selenium.webdriver.support.ui as ui wait = ui.WebDriverWait(driver, 10) wait.until(lambda d: d.execute_script('return document.readyState') == 'complete') driver.execute_script("window.scrollBy(0, 1000)") time.sleep(1) driver.quit()
Quick Reference
Use these JavaScript commands inside execute_script to scroll:
| Command | Effect |
|---|---|
window.scrollBy(0, 500) | Scroll down 500 pixels |
window.scrollTo(0, 0) | Scroll to top of page |
window.scrollTo(0, document.body.scrollHeight) | Scroll to bottom of page |
Remember to wait after scrolling if page content loads dynamically.
| Command | Effect |
|---|---|
| window.scrollBy(0, 500) | Scroll down 500 pixels |
| window.scrollTo(0, 0) | Scroll to top of page |
| window.scrollTo(0, document.body.scrollHeight) | Scroll to bottom of page |
Key Takeaways
Use driver.execute_script with JavaScript scroll commands to scroll pages in Selenium Python.
Wait for the page to load fully before scrolling to avoid errors.
Use time.sleep or explicit waits after scrolling to allow page content to load.
Scroll by pixels or to exact positions using window.scrollBy and window.scrollTo.
Avoid deprecated Selenium scroll methods; prefer JavaScript execution for scrolling.