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 selenium.webdriver.common.by import By
DRIVER_PATH = '/path/to/chromedriver'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
driver.get("http://www.scrapingbee.com")
# 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)