How to scroll to an element in Selenium?

You can scroll to an element in Selenium by making use of the execute_script method and passing in a Javascript expression to do the actual scrolling. You can use any supported Selenium selectors to target any WebElement and then pass that to the execute_script as an argument.

Here is some example code that navigates to the ScrapingBee homepage and scrolls to the footer tag:

from selenium import webdriver
from import By

DRIVER_PATH = '/path/to/chromedriver'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)


# Javascript expression to scroll to a particular element
# arguments[0] refers to the first argument that is later passed
# in to execute_script method
js_code = "arguments[0].scrollIntoView();"

# The WebElement you want to scroll to
element = driver.find_element(By.TAG_NAME, 'footer')

# Execute the JS script
driver.execute_script(js_code, element)

Related Selenium web scraping questions: