How to Scroll a Web Page Using Selenium WebDriver in Python

How to Scroll a Web Page Using Selenium WebDriver in Python

21 May 2024 Stephan Petzl Leave a comment Tech-Help

When automating web interactions with Selenium WebDriver, scrolling through a web page is a common requirement, especially for pages with infinite scrolling like social media feeds or search results. Here, we will guide you through various methods to achieve this using Python.

Using JavaScript Execution

The most flexible approach to scroll a web page is by executing JavaScript. You can use the execute_script method to run JavaScript commands directly in the browser.

Scroll to a Specific Position

To scroll to a specific vertical position, use:

driver.execute_script("window.scrollTo(0, Y)")

Here, Y is the vertical pixel value you want to scroll to.

Scroll to the Bottom of the Page

To scroll to the bottom of the page, use:

driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")

Handling Infinite Scrolling

For pages with infinite scrolling, such as social media feeds, you can use a loop to continuously scroll until no more content is loaded.

import time

SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height

Using Selenium Keys

You can simulate key presses like PAGE_DOWN or END to scroll the page:

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

html = driver.find_element(By.TAG_NAME, 'html')
html.send_keys(Keys.END)

Scrolling to an Element

If you need to scroll to a specific element, you can use:

element = driver.find_element(By.XPATH, "your element xpath")
driver.execute_script("arguments[0].scrollIntoView();", element)

Conclusion

By utilizing these methods, you can effectively manage scrolling in your Selenium WebDriver scripts, ensuring that your automated tasks can interact with all elements on a page, even those that are not initially visible.

For those looking to streamline their mobile app testing process, consider using Repeato. Repeato is a no-code test automation tool for iOS and Android that enables you to create, run, and maintain automated tests for your apps effortlessly. With features like computer vision and AI, it ensures fast and efficient test execution. Additionally, Repeato allows testing websites within an Android emulator or device, making it a versatile tool for your automation needs.

Like this article? there’s more where that came from!