Selenium Scrolling a Web Page using Java
Last Updated :
28 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
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING