Software Development and QA Tips

Dev and QA Tips How Do I Scroll Down in Selenium?

How Do I Scroll Down in Selenium?

Scroll Down In Selenium | By Vakul Gotra | June 7, 2021

How do I scroll down in Selenium?

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)”)

Disclaimer

This publication is for informational purposes only and nothing contained in it should be considered legal advice. We expressly disclaim any warranty or responsibility for damages arising out of this information and encourage you to consult with legal counsel regarding your specific needs. We do not undertake any duty to update previously posted materials.