How to scroll to an element in Selenium?

by scrapecrow Nov 25, 2022

The Selenium headless browser automation library enables full web automation control, and one of these automation tasks is scrolls. In this guide, we'll explain using Python selenium scroll to element. Let's get started!

Installation

Let's start with the installation process. We'll be using the below packages:

  • Selenium: The Selenium Python client to communicate with the web driver API.
  • webdriver-manager: To automatically download the Selenium web driver binaries itself
    The above packages can be installed using the below pip command:
pip install selenium webdriver-manager

Next, use the webdriver-amanger to automatically download the Selenium web driver:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

The above script will install the Chrome browser driver. However, other browsers are supported. See the official documentation for the available engines.

Using scrollIntoView

To scroll into a specific element using Sleneium, we can follow the below steps:

  • Select a particular element to scroll into using its equivalent CSS or XPath selector
  • Use the scrollIntoView method with the selected HTML element

Let's apply the above steps within our Python Selenium code:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService

driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

driver.get("https://web-scraping.dev/product/1")

# scroll to an element at the bottom of the page
element = driver.find_element(By.CSS_SELECTOR, 'footer')

# execute scrollIntoView script with our element as the argument:
driver.execute_script(
    "arguments[0].scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'end' });", 
    element
)

time.sleep(5) # observe the new view
driver.close() # close the browser

Above, we start by selecting a web element at the bottom of them page using CSS selectors. Then, we use the execute_script method to execute JavaScript code to scroll to the element the scrollIntoView function. 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.

For further scrolling techniques, refer to our guide on scrolling with selenium.

Related Articles

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.

HEADLESS-BROWSER
PUPPETEER
SELENIUM
NODEJS
PLAYWRIGHT
PYTHON
How to Scrape With Headless Firefox

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.

PYTHON
HEADLESS-BROWSER
SELENIUM
TOOLS
Selenium Wire Tutorial: Intercept 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.

PYTHON
SCRAPY
HEADLESS-BROWSER
SELENIUM
Web Scraping Dynamic Web Pages With Scrapy Selenium

How to use Headless Chrome Extensions for Web Scraping

In this article, we'll explore different useful Chrome extensions for web scraping. We'll also explain how to install Chrome extensions with various headless browser libraries, such as Selenium, Playwright and Puppeteer.

PYTHON
NODEJS
TOOLS
PLAYWRIGHT
PUPPETEER
SELENIUM
How to use Headless Chrome Extensions for Web Scraping

Intro to Web Scraping Using Selenium Grid

In this guide, you will learn about installing and configuring Selenium Grid with Docker and how to use it for web scraping at scale.

SELENIUM
PYTHON
Intro to Web Scraping Using Selenium Grid

How to Scrape Google Maps

We'll take a look at to find businesses through Google Maps search system and how to scrape their details using either Selenium, Playwright or ScrapFly's javascript rendering feature - all of that in Python.

SCRAPEGUIDE
PYTHON
SELENIUM
PLAYWRIGHT
How to Scrape Google Maps