To scroll down using Selenium in Python, you can use the Selenium webdriver execute_script() function which executes JavaScript code in the browser.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://daztech.com/")
#Scroll to Bottom of Webpage
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
The Selenium Python module gives you the tools you need to be able to automate many tasks when working with web browser.
When working with web browsers, it’s possible you may want to scroll down to the bottom of a webpage or scroll down to a particular pixel height.
To scroll down using Selenium in your Python code, you can use the Selenium webdriver execute_script() function. execute_script() is a versatile function and allows you to execute JavaScript code in the browser.
With JavaScript, we can easily scroll down in a browser.
How to Scroll Down to Bottom of Webpage Using Selenium
To scroll down to the bottom of a webpage using Selenium, you can use the execute_script() function to execute the JavaScript function window.scrollTo() and pass ‘document.body.scrollHeight’ for the second parameter.
Below shows you a simple example of how you can use Selenium to scroll down to the bottom of a webpage.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://daztech.com/")
#Scroll to Bottom of Webpage
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
How to Scroll Down to Specific Height Using Selenium
If you want to scroll down to a specific pixel height, then you can change the second parameter of the window.scrollTo() function.
For example, if you want to scroll down to pixel 600, then pass ‘600’ to window.scrollTo().
Below shows you how to use Selenium to scroll down to a specific height in your Python code.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://daztech.com/")
#Scroll to Pixel 600 of Webpage
driver.execute_script("window.scrollTo(0,600))
Hopefully this article has been useful for you to learn how to scroll down using Selenium in Python.