How to Scroll an Element into View in Selenium?
Last Updated :
23 Jul, 2025
A high-level programming language that helps users in building web applications is called Java. It is not only used for creating web applications but it can also be used for automating web applications through various automation tools. Selenium is one such tool, which gives users the capability to automate from scratch to end, whether it is opening a web [age, clicking on an element, scrolling the web page, etc. In this article, we will focus on scrolling an element into view through two different approaches, i.e., using JavascriptExecutor and Actions.
There are 2 different approaches to scroll an element into view in Selenium, which are as follows:
- Using JavascriptExecutorDriver
- Using Actions
Using JavascriptExecutorDriver
The JavaScriptExecutorDriver is used when certain actions cannot be performed through the usage of Selenium in-built functions and we need some external class to perform such actions. One such action is scrolling of elements into view, that cannot be performed by Selenium in-built functions. In this approach, we will see how we can scroll an element into view using JavascriptExecutorDriver in Selenium.
Syntax:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();", element);
Here,
- element: It is the element that has to be brought into view by scrolling.
Example of JavascriptExecutorDriver:
In this example, we have opened the Geeks For Geeks (link) website, and then we have found the element having the text 'Problem of the day'. Further, we have scrolled to that element using JavascriptExecutorDriver.
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 of JavascriptExecutorDriver:
Using JavascriptExecutorDriverUsing Actions
The Actions is used when the users want web driver to perform numerous actions one after another. This method is great but has certain limitations with it as the moveToElement function which is used in it moves the mouse cursor to the center of the element. It doesn't make sure if the element is visible within the viewport or not. Thus, limiting the webdriver to perform further action on the element. In this approach, we will see how we can scroll an element into view using Actions in Selenium.
Syntax:
Actions a = new Actions(driver);
a.moveToElement(element).perform();
Here,
- element: It is the element that has to be brought into view by scrolling.
Example of Actions:
In this example, we have opened the Geeks For Geeks (link) website, and then we have found the element having the text 'Problem of the day'. Further, we have scrolled to that element using Actions.
Java
//Import selenium libraries
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.By;
public class selenium4 {
public static void main(String[] args) {
// State the chromedriver URL
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
// Define and initiate the chrome driver
WebDriver driver = new ChromeDriver();
// Launch the website
driver.get("https://www.geeksforgeeks.org/");
// Maximize the screen
driver.manage().window().maximize();
// Stating the actions
Actions a = new Actions(driver);
// Find Problem of the day text
WebElement element = driver.findElement(By.xpath("// *[contains(text(),'Problem of the day')]"));
// Scroll to specific position
a.moveToElement(element).perform();
}
}
Output of Actions:
Using Action ClassConclusion
In conclusion, Selenium can handle only the elements that are visible within the viewport. Thus, scrolling of elements is very crucial for the elements currently out of viewport so that further actions of that element could be performed successfully. The technique of scrolling through JavascriptExecutorDriver is the recommended approach as it gives users the choice to control the web page through Javascript code and is comparatively faster than the other approach. I hope the various ways stated in the article will help you in scrolling an element into the view in Selenium.
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