How to scroll to an element in Selenium?

To scroll to an element in Selenium we can find the HTML element using CSS or XPath selectors and execute javascript scrollIntoView() function:

from selenium import webdriver
from import By

driver = webdriver.Chrome()

# find element to scroll to. In this example we select last element with product class:
element = driver.find_elements(By.CSS_SELECTOR, '.products .product')[-1]
# execute scrollIntoView script with our element as the argument:
    "arguments[0].scrollIntoView(scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'end' });", 

In the example above we're using scrollIntoView with smooth type of scrolling which performs more like a real user. We also specify block and inline arguments to end value to scroll to the bottom of the horizontal right of the element - this ensures highest visibility of the element. For more see browser's scrollIntoView documentation

Question tagged: Selenium, Python

Related Posts

How to Scrape With Headless Firefox

Discover how to use headless Firefox with Selenium, Playwright, and Puppeteer for web scraping, including practical examples for each library.

Selenium Wire Tutorial: Intercept Background Requests

In this guide, we'll explore web scraping with Selenium Wire. We'll define what it is, how to install it, and how to use it to inspect and manipulate background requests.

Web Scraping Dynamic Web Pages With Scrapy Selenium

Learn how to scrape dynamic web pages with Scrapy Selenium. You will also learn how to use Scrapy Selenium for common scraping use cases, such as waiting for elements, clicking buttons and scrolling.