Home > Backend Development > Python Tutorial > How to Scroll Web Pages with Selenium WebDriver in Python?

How to Scroll Web Pages with Selenium WebDriver in Python?

Susan Sarandon
Release: 2024-12-13 16:00:24
Original
703 people have browsed it

How to Scroll Web Pages with Selenium WebDriver in Python?

Scrolling a Web Page with Selenium WebDriver in Python

In Selenium WebDriver, the ability to scroll down a web page becomes essential when navigating dynamic content or extracting data from extensive lists. To achieve this scrolling functionality in Python, several approaches can be employed.

Method 1: Explicit Scrolling

To scroll to a specific position on the page, use the execute_script() method:

driver.execute_script("window.scrollTo(0, Y)")
Copy after login

where Y represents the height to scroll to.

Method 2: Scrolling to the Bottom

To scroll to the bottom of the page, execute the following script:

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
Copy after login

Method 3: Scrolling Infinite Loading Pages

For pages with infinite loading, such as social media feeds, use this loop until no further scrolling is possible:

SCROLL_PAUSE_TIME = 0.5

last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(SCROLL_PAUSE_TIME)
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
Copy after login

Method 4: Using Keyboard Keys

Alternatively, to scroll down one page, simulate the Page Down keypress:

label.send_keys(Keys.PAGE_DOWN)
Copy after login

The above is the detailed content of How to Scroll Web Pages with Selenium WebDriver in Python?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template