Selenium Scrolling a Web Page
Last Updated :
24 Apr, 2025
Selenium is an open-source program that automates web browsers. It offers a single interface that enables us to create test scripts in several different programming languages, including Ruby, Java, NodeJS, PHP, Perl, Python, and C#. It is also compatible with different Operating Systems like Windows, Mac, Linux, etc.
Selenium ecosystem consists of four major tools namely - Selenium WebDriver, Selenium Grid, Selenium IDE, and Selenium RC. Selenium Webdriver is mainly used to execute the scripts according to the browser we are using.
In this article, we will see various methods by which scrolling a webpage is possible using Selenium while we test our applications.
Prerequisites:
pip install selenium
Install SeleniumSteps to Scroll a Webpage using Selenium:
Selenium has many different methods for scrolling a webpage. There are several options for how we want to scroll across a webpage: we can scroll through the webpage slowly with a defined speed, go up or down to a specific element, or go straight to the top or bottom of the page. For all the ways of scrolling, the initial setup and imports remain the same and are mentioned below.
1. Import the required packages.
The webdriver module will be used to run the Webdriver Implementations for Chrome browser. Time package will be used for functions like sleep for clear visibility of what is happening on the webpage.
from selenium import webdriver
import time
2. Define the necessary string constants.
PATH variable is the location of the chrome driver we have installed and will be used in initializing the instance of webdriver. URL is the example webpage used for the demo.
PATH = r"C:\Users\VRINDA\Desktop\chromedriver_win32\chromedriver.exe"
URL = "https://vrii14.github.io/"
3. Initializing and defining Chrome Options.
These options ensure we do not get any unnecessary warnings while running Selenium due to some Chromedriver issues.
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
4. Creating an instance of the webdriver and opening the URL webpage.
browser = webdriver.Chrome(PATH, options=options)
browser.get(URL);
browser.maximize_window();
Here, we create an instance of the Chrome webdriver in the first line using the PATH variable and the options. In the second line, we open the webpage of the URL provided. Third line maximizes the Chrome window.
Scroll the Webpage:
1. Scrolling through the webpage slowly (infinite till end of page):
To scroll through the webpage slowly, we create a while loop and use window.scrollBy function to scroll down by that many pixels down and followed by time.sleep function for the scrolling to be slow and visible. We also use a count variable "stopScrolling" for the while loop to end after reaching the end of webpage. The number "120" will according to the length of the webpage and can be determined by trial and error or printing the counter and checking at value has the webpage ended.
Below is the Python program to scroll through the webpage slowly:
Python
# Python program to scroll through
# the webpage slowly
from selenium import webdriver
import time
# string constants
PATH = r"C:\Users\VRINDA\Desktop\chromedriver_win32\chromedriver.exe"
URL = "https://vrii14.github.io/"
# chrome options
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
try:
browser = webdriver.Chrome(PATH, options=options)
browser.get(URL)
browser.maximize_window()
# sleep for visibility of change
time.sleep(2)
# scrolling down slowly
stopScrolling = 0
while True:
stopScrolling += 1
browser.execute_script("window.scrollBy(0,40)")
time.sleep(0.5)
if stopScrolling > 120:
break
time.sleep(3)
finally:
browser.quit()
Output:
2. Scroll to the bottom of the webpage:
For scrolling to the bottom of the page we use the windows.scrollTo function and specify the end of webpage pixel value with document.body.scrollHeight which is defined already.
Method:
window.scrollTo(0,document.body.scrollHeight)
Code:
Python
from selenium import webdriver
import time
# string constants
PATH = r"C:\Users\VRINDA\Desktop\chromedriver_win32\chromedriver.exe"
URL = "https://vrii14.github.io/"
# chrome options
options = webdriver.ChromeOptions();
options.add_experimental_option('excludeSwitches', ['enable-logging']);
try:
browser = webdriver.Chrome(PATH, options=options);
browser.get(URL);
browser.maximize_window();
# sleep for visibility of change
time.sleep(2);
# scroll to the bottom
browser.execute_script("window.scrollTo(0,document.body.scrollHeight)");
time.sleep(3);
finally:
browser.quit()
Output:
3. Scroll based on the visibility of the Web element on the page:
For scrolling to the particular element, we need to first find the element by any specific characteristic like id, class, xpath, link text, etc and the value can be found by inspecting the webpage. Here, we use the id of the element to find it with this function browser.find_element_by_id(). After getting the element, we can scroll to the particular element using the scrollItoView function and providing the element as the argument.
Method:
scrollIntoView();
Code:
Python
from selenium import webdriver
import time
# string constants
PATH = r"C:\Users\VRINDA\Desktop\chromedriver_win32\chromedriver.exe"
URL = "https://vrii14.github.io/"
# chrome options
options = webdriver.ChromeOptions();
options.add_experimental_option('excludeSwitches', ['enable-logging']);
try:
browser = webdriver.Chrome(PATH, options=options);
browser.get(URL);
browser.maximize_window();
# sleep for visibility of change
time.sleep(2);
# scroll to the top
element = browser.find_element_by_id("journey");
browser.execute_script('arguments[0].scrollIntoView(true)', element);
time.sleep(3);
finally:
browser.quit()
Output:
4. Scroll down a page by specified pixels:
For scrolling by a specified number of pixels, we use the function window.scrollBy(0,x) and specify the number of pixels in place of x. If we need to scroll down by the number of pixels, then the value of x is positive and if we need to scroll up with same number of pixels we can negate the value of x.
Method to be used:
scrollBy();
Code:
Python
from selenium import webdriver
import time
# string constants
PATH = r"C:\Users\VRINDA\Desktop\chromedriver_win32\chromedriver.exe"
URL = "https://vrii14.github.io/"
# chrome options
options = webdriver.ChromeOptions();
options.add_experimental_option('excludeSwitches', ['enable-logging']);
try:
browser = webdriver.Chrome(PATH, options=options);
browser.get(URL);
browser.maximize_window();
# sleep for visibility of change
time.sleep(2);
browser.execute_script("window.scrollBy(0,850)");
time.sleep(3);
finally:
browser.quit()
Output:
5. Scroll to the top of the page:
For scrolling to top of the webpage, we need to set the pixel values to coordinates of either (0,0) or (document.body.scrollHeight, 0). For visibility of scrolling to the top of the page, we need to first scroll down and then to the up again.
Method to be used:
scrollTo(0,0);
Code:
Python
from selenium import webdriver
import time
# string constants
PATH = r"C:\Users\VRINDA\Desktop\chromedriver_win32\chromedriver.exe"
URL = "https://vrii14.github.io/"
# chrome options
options = webdriver.ChromeOptions();
options.add_experimental_option('excludeSwitches', ['enable-logging']);
try:
browser = webdriver.Chrome(PATH, options=options);
browser.get(URL);
browser.maximize_window();
# sleep for visibility of change
time.sleep(2);
# scroll to the top
browser.execute_script("window.scrollTo(0, 0)");
time.sleep(3);
finally:
browser.quit()
Output:
6. Scroll a webpage horizontally:
For scrolling a page horizontally, we just have to change the x-coordinate in the windows.scrollBy() function instead of y-coordinate. The value of x coordinate determines the pixels by how much it will scroll horizontally.
Method:
window.scrollBy(850, 0);
Code:
Python
from selenium import webdriver
import time
# string constants
PATH = r"C:\Users\VRINDA\Desktop\chromedriver_win32\chromedriver.exe"
URL = "https://vrii14.github.io/"
# chrome options
options = webdriver.ChromeOptions();
options.add_experimental_option('excludeSwitches', ['enable-logging']);
try:
browser = webdriver.Chrome(PATH, options=options);
browser.get(URL);
browser.maximize_window();
# sleep for visibility of change
time.sleep(2);
#scroll the page horizontally
browser.execute_script("window.scrollBy(850, 0)");
time.sleep(3);
finally:
browser.quit()
Output:
Close the browser:
browser.quit()
This will finally close the browser.
Conclusion:
We have seen how we can use selenium scrolling in multiple ways to help test our applications.
Similar Reads
Selenium Scrolling a Web Page using Java
An open-source framework that is used for automating web applications is known as Selenium. Selenium handles various operations, like opening of website, clicking buttons, scrolling the webpage, etc. In this article, we have defined the numerous ways to scroll a webpage using Selenium in Java. Table
7 min read
Web Scraping Tables with Selenium and Python
Selenium is the automation software testing tool that obtains the website, performs various actions, or obtains the data from the website. It was chiefly developed for easing the testing work by automating web applications. Nowadays, apart from being used for testing, it can also be used for making
4 min read
Selenium WebDriver-Handling Alerts
Selenium is one of the most popular and powerful tools for automated web applications. Selenium is widely used for automating user interactions like filling out forms, navigating to a website clicking on a button, etc. While we are dealing with automating user interactions one of the most common sce
5 min read
Selenium - Search for text on page
Prerequisite:- Selenium Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. In
1 min read
Selenium IDE-Login Test
In today's world of software development, ensuring seamless user journeys is crucial. Login functionality lies at the heart of most web applications, making robust testing indispensable. This guide introduces us to the Selenium IDE, a powerful tool for automating login tests and streaming our softwa
6 min read
Selenium Program to Login to a Specific Web Page
Selenium RC (Selenium Remote Control) is one of the Selenium Components which was developed by Paul Hammant. It is an automation testing tool for web applications. To create test scripts, testers are allowed to use many programming languages such as Java, Python, Ruby, C#, JavaScript, Perl, and PHP.
4 min read
Selenium WebDriver-Installation
Selenium WebDriver is a powerful tool for automating web applications for testing purposes. It allows developers and testers to write automated tests in various programming languages like Java, Python, C#, etc. Also, it supports different browsers like Firefox, Chrome, Edge, etc. for testing. Approa
2 min read
Selenium IDE-Installation
Selenium IDE Installation is a straightforward process that enables you to start automated browser testing quickly. Selenium IDE (Integrated Development Environment) is a popular open-source tool that allows testers and developers to record, edit, and debug tests. It is particularly useful for those
5 min read
Scroll Web Page Base On Pixel Method Using Selenium in Python
Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. A Scrollbar is helped you
2 min read
Selenium WebDriver Event Listener
Testing Websites often includes testing multiple pages in the website. "Selenium" is one of the most popular test automated frameworks used to test multiple web pages provides numerous functionalities and enables the interaction between web pages. The name "Listeners" suggests that they can listen t
5 min read