Scrolling Web Pages with Selenium Webdriver in Python
When accessing web pages with dynamically loaded content, such as Facebook friends lists, scrolling is often necessary to retrieve all available data. Selenium Webdriver provides a range of methods to enable scrolling in Python.
Solution 1: Explicitly Scrolling Defined Distance
To scroll to a specific vertical position, utilize the following syntax:
driver.execute_script("window.scrollTo(0, Y)")
Replace Y with the desired scroll height, typically 1080 for a full HD monitor.
Solution 2: Scrolling to Page Bottom
To scroll to the bottom of the page:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
Solution 3: Scrolling Pages with Infinite Loading
For social media platforms like Facebook with infinite scrolling:
SCROLL_PAUSE_TIME = 0.5 # Get initial 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);") # Pause to allow loading time.sleep(SCROLL_PAUSE_TIME) # Check if scroll height has changed new_height = driver.execute_script("return document.body.scrollHeight") if new_height == last_height: break last_height = new_height
Solution 4: Using Keyboard Shortcuts
Identify an element and apply the following:
label.sendKeys(Keys.PAGE_DOWN)
The above is the detailed content of How Can I Scroll Web Pages Using Selenium WebDriver in Python?. For more information, please follow other related articles on the PHP Chinese website!