Software Development and QA Tips By QASource Experts

How Do I Scroll Down in Selenium?

Written by Timothy Joseph | Jun 7, 2021 4:00:00 PM

To scroll down a page, use the driver.execute_script() method to locate the particular position by providing the parameters.

The code below can be used by teams to scroll down in Selenium

#import “webdriver”
import selenium webdriver

#load chrome drivers from the path where drivers are installed
driver = webdriver.Chrome(executable_path='Path of exe driver\chromedriver.exe')

#maximize the browser
driver.maximize_window()

#navigate to the url
driver.get(“https://www.worldometers.info/geography/flags-of-the-world/”)

#Scroll down page by Pixel
driver.execute_script(“window.scrollBy(0,9000)”, “”)

#Scroll page till element is visible
countryFlag = driver.find_element_by_xpath(“xpath of the element”)

driver.execute_script(“arguments[0].scrollIntoView();”, countryFlag)

#Scroll till end of the page
driver.execute_script(“window.scrollBy(0, document.body.scrollHeight)”)