Selenium Scrolling a Web Page using Java
Last Updated :
24 Apr, 2025
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.
Selenium Scrolling a Web Page using Java
Now, we will discuss the various ways to scroll the webpage using Selenium webdriver in Java.
Scroll Down to the Page’s Bottom
If the user knows the element he is finding for further actions is at the bottom of the page, then this is the best approach. In this approach, we have opened the Geeks For Geeks website (link) and then scrolled to the bottom of the page.
Java
// Importing the Selenium libraries
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class selenium3 {
public static void main(String[] args)
{
// specify the location of the driver
System.setProperty(
"webdriver.chrome.driver","C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
// Initialising the driver
WebDriver driver = new ChromeDriver();
// launch the website
driver.get("https://www.geeksforgeeks.org/");
// Maximize the screen
driver.manage().window().maximize();
// Stating the Javascript Executor driver
JavascriptExecutor js = (JavascriptExecutor)driver;
// Scroll to bottom of page
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
}
}
Output:

Scroll Based on the Visibility of the Web Element on the Page
Whenever the user wants to scroll the page till the driver finds the position of the element on the webpage, it is the best approach. In this approach, we opened the Geeks For Geeks website (link) and then scrolled till the webdriver found the element containing the text 'Problem of the day' using the contains and findElement function.
Java
// Importing the Selenium libraries
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
public class selenium3 {
public static void main(String[] args) {
//specify the location of the driver
System.setProperty("webdriver.chrome.driver","C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
//Initialising the driver
WebDriver driver = new ChromeDriver();
//launch the website
driver.get("https://www.geeksforgeeks.org/");
// Maximize the screen
driver.manage().window().maximize();
// Stating the Javascript Executor driver
JavascriptExecutor js = (JavascriptExecutor) driver;
// Find Problem of the day text
WebElement element = driver.findElement(By.xpath("// *[contains(text(),'Problem of the day')]"));
// Scroll to the specific position
js.executeScript("arguments[0].scrollIntoView();", element);
}
}
Output:

Scroll a Webpage Both Horizontally and Vertically
This is the best approach if the user knows he needs to scroll the webpage horizontally as well as vertically to find the web element. In this approach, we have opened the Geeks For Geeks website (link) and then scrolled the webpage both horizontally and vertically.
- Horizontal Scroll: Here we have scrolled the webpage horizontally by 50 pixels.
- Vertical Scroll: Here we have scrolled the webpage horizontally by 1500 pixels.
Java
// Importing the Selenium libraries
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class selenium3 {
public static void main(String[] args) {
//specify the location of the driver
System.setProperty("webdriver.chrome.driver","C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
//Initialising the driver
WebDriver driver = new ChromeDriver();
//launch the website
driver.get("https://www.geeksforgeeks.org/");
// Stating the Javascript Executor driver
JavascriptExecutor js = (JavascriptExecutor) driver;
// Scroll both horizontally and vertically
js.executeScript("window.scrollBy(50,1500)");
}
}
Output:

Scroll a Webpage with Infinite Scrolling
When the webpage is too large and the user wants to scroll a webpage infinitely till he reaches the end of the webpage or at the specific position he wants, then he can use this approach. In this approach, we opened the Geeks For Geeks website (link) and then scrolled infinitely till he reached the end of the webpage using a while loop and break feature.
Java
// Importing Selenium libraries
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class selenium8 {
public static void main(String[] args) {
//specify the location of the driver
System.setProperty("webdriver.chrome.driver","C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
//Initialising the driver
WebDriver driver = new ChromeDriver();
// Open the Geeks For Geeks website
driver.get("https://www.geeksforgeeks.org/");
// Maximize the screen
driver.manage().window().maximize();
// Add implicit wait of 10 seconds
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Stating the Javascript Executor driver
JavascriptExecutor js = (JavascriptExecutor) driver;
// Return the total height of the webpage
long initialHeight = (long) js.executeScript("return document.body.scrollHeight");
// Start a while loop
while(true){
// Do infinite scrolling
js.executeScript("window.scrollTo(0,document.body.scrollHeight)");
// Add implicit wait of 10 seconds
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// Get current height of the page
long currentHeight = (long) js.executeScript("return document.body.scrollHeight");
// Steop when initial height equals to current height
if(initialHeight == currentHeight) {
break;
}
initialHeight = currentHeight;
}
}
}
Output:

Scroll to the Top of the Page
Sometimes the webpage is too large and the user has scrolled to a specific position, and then if the user has to scroll back to the top of the page, this is the best approach. In this approach, we have opened the Geeks For Geeks website (link) and then scrolled to the top of the page.
Java
// Importing the Selenium libraries
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class selenium3 {
public static void main(String[] args) {
//specify the location of the driver
System.setProperty("webdriver.chrome.driver","C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
//Initialising the driver
WebDriver driver = new ChromeDriver();
//launch the website
driver.get("https://www.geeksforgeeks.org/");
// Maximize the screen
driver.manage().window().maximize();
// Stating the Javascript Executor driver
JavascriptExecutor js = (JavascriptExecutor) driver;
// Scroll to top of page
js.executeScript("window.scrollTo(document.body.scrollHeight,0)");
}
}
Output:

Scroll Down a Page by Specified Pixels
This is the best approach if the user knows the exact dimensions of the webpage, where the web element is present. In this approach, we have opened the Geeks For Geeks website (link) and then scrolled the webpage both horizontally and vertically.
Java
// Importing the Selenium libraries
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class selenium3 {
public static void main(String[] args) {
//specify the location of the driver
System.setProperty("webdriver.chrome.driver","C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
//Initialising the driver
WebDriver driver = new ChromeDriver();
//launch the website
driver.get("https://www.geeksforgeeks.org/");
// Maximize the screen
driver.manage().window().maximize();
// Stating the Javascript Executor driver
JavascriptExecutor js = (JavascriptExecutor) driver;
// Scrolling a webpage vertically
js.executeScript("window.scrollBy(40,1000)");
}
}
Output:

Conclusion
In conclusion, scrolling a webpage using Selenium WebDriver in Java can happen in various ways, from specific pixels, to the bottom of the page, to the top of the page, to infinite scrolling, etc. I hope after reading the above article, you will be able to do all types of scrolling using Selenium in Java.
Similar Reads
Selenium Scrolling a Web Page
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
6 min read
How to Save a Web Page with Selenium using Java?
Selenium is widely known for automating browser interactions, but it can also be used to save web pages directly. This capability is particularly useful when you need to archive web content, save dynamic pages that change frequently, or scrape and store HTML for later analysis. In this tutorial, weâ
3 min read
Gmail Login using Java Selenium
Automating Gmail login using Selenium WebDriver and Java is a common task for beginners learning web automation. Itâs an excellent exercise to understand how Selenium interacts with web elements and handles user inputs. This article will guide you through the process of automating Gmail login, provi
4 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
Key press in (Ctrl+A) Selenium WebDriver using java
Selenium WebDriver is a powerful tool for automating web applications, and it offers various ways to simulate user actions such as clicking, typing, and even pressing keyboard shortcuts. One of the common actions is selecting all text or elements on a page using the "Ctrl+A" key press. This is usefu
3 min read
How can we Find an Element using Selenium?
Selenium is one of the most popular and powerful tools for automating web applications. Selenium is widely used for automating user interactions on a web application like clicking on a button, navigating to a web page, filling out web forms, and many more. But to interact with a web application we f
6 min read
Search and Play Youtube Music with Selenium using java
In this article we will be playing music from YouTube Music is a music sharing website which is similar to Spotify created by Google. In this article we will be playing a song from YouTube Music using Google Chrome by Selenium and Java.Example to Search and Play YouTube Music with Selenium using jav
2 min read
How to Take a Screenshot in Selenium WebDriver Using Java?
Selenium WebDriver is a collection of open-source APIs used to automate a web application's testing. To capture a screenshot in Selenium, one must utilize the Takes Screenshot method. This notifies WebDriver that it should take a screenshot in Selenium and store it. Selenium WebDriver tool is used t
3 min read
Reading JavaScript variables using Selenium WebDriver
In web automation testing, interacting with JavaScript variables on a webpage can be essential for validating specific conditions. Selenium WebDriver provides a way to execute JavaScript directly on the browser using the `executeScript()` method. This feature allows us to read JavaScript variables a
2 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