Selenium – Scroll down

Python Selenium – Scroll down

To scroll down a webpage using Selenium in Python, you can use the execute_script() method to run JavaScript code that performs the scrolling action.

The syntax of the execute_script() method with the JavaScript code is given below.

driver.execute_script("window.scrollTo(0, window.innerHeight);")

This code scrolls down the page by the height of the browser window. window.scrollTo() method is used to scroll to specific position, and we have given window.innerHeight which is height of the current browser window.

To scroll down to the end of the page, please refer to the tutorial – Python Selenium – Scroll to end of the page.

Example

In the following program, we load the URL https://pythonexamples.org, and scroll down the page by the height of the window.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
import time

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/')

# Wait for the page to load (optional)
time.sleep(2)

# Scroll down using JavaScript
driver.execute_script("window.scrollTo(0, window.innerHeight);")

# Wait for a moment to let the page scroll (optional)
time.sleep(2)

# Close the WebDriver
driver.quit()

Output

Summary

In this Python Selenium tutorial, we have seen how to scroll down one time, by the height of the window, with examples.

Related Tutorials

Quiz on Selenium

Q1. What is the method used to find web elements in Selenium Python?

Not answered

Q2. Which of the following is not a method to wait for a web element to load in Selenium Python?

Not answered

Q3. Which of the following is not a commonly used assertion method in Selenium Python?

Not answered

Q4. What is the method used to switch to a different frame in Selenium Python?

Not answered

Q5. Which of the following is not a commonly used web element locator in Selenium Python?

Not answered
Code copied to clipboard successfully 👍