How to Scroll a Web Page Using Selenium WebDriver in Python
To extract all friend IDs from the AJAX script on Facebook's user friends page, it is necessary to scroll down to gather all the information. Here's how to achieve this in Selenium using Python:
driver.execute_script("window.scrollTo(0, Y)")
where Y represents the desired pixel height.
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
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
label.sendKeys(Keys.PAGE_DOWN);
By utilizing these techniques, you can effectively scroll through web pages and extract the desired data using Selenium and Python.
The above is the detailed content of How Can I Scroll Web Pages with Selenium WebDriver in Python?. For more information, please follow other related articles on the PHP Chinese website!