Home >Backend Development >Python Tutorial >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)")
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);")
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
Method 4: Using Keyboard Keys
Alternatively, to scroll down one page, simulate the Page Down keypress:
label.send_keys(Keys.PAGE_DOWN)
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!