How to Scroll an Element into View in Selenium?
Last Updated :
12 Apr, 2024
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
How to wait until an element is present in Selenium?
Selenium is a powerful tool for automating web browsers, often used for testing web applications. However, web pages that load content dynamically can present challenges, especially when you need to interact with elements that may not be immediately available. In such cases, waiting for an element t
2 min read
How To Scroll To An HTML Element In NextJS?
NextJS has various features that enable smooth scrolling to HTML elements. One method involves using useRef to create a reference to the target element and scrollIntoView to achieve smooth scrolling. Another method, using useEffect and window.scrollBy, sets up an event listener on a button for smoot
3 min read
How to Fix Selenium's "Element Is Not Clickable at Point"
When automating web applications with Selenium WebDriver, you may encounter various exceptions that can halt your tests. One common issue that many testers face is the "Element is not clickable at point" exception. This error can be frustrating, especially when you're trying to interact with an elem
5 min read
How to Get Child Element in Selenium?
The tool that reduces human effort and makes the work relatively easier by automating the browser is known as Selenium. The elements which are located within some other elements, such elements are called child elements. Sometimes, we have to handle such elements, such as values of radio buttons, lis
5 min read
How to check if an element exists with Python Selenium?
Selenium is one of the most powerful and widely used tools for web automating web applications. Whether you're a software developer or a QA tester, Selenium is an important tool to have in your toolkit. Selenium is widely used for automating user interactions like clicking buttons, filling out the f
7 min read
screenshot_as_png element method - Selenium Python
Seleniumâs Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout - Navigating links using get method â Selenium Python. Just bein
2 min read
How to handle multiple windows in Selenium?
Handling multiple windows in Selenium is crucial when dealing with web applications that open new tabs or windows during interactions. Whether you're testing applications with pop-ups, modals, or new browser tabs, Selenium WebDriver provides efficient methods to switch between multiple windows or ta
7 min read
How to click on hidden element in Selenium WebDriver?
In Selenium WebDriver, interacting with web elements is key to automating web applications. However, certain elements on a webpage might be hidden or obscured, making it challenging to interact with them directly. In such cases, developers and testers often need to use alternative methods, such as J
3 min read
How to Scroll to an Element Inside a Div using JavaScript?
To scroll to an element within a div using JavaScript, set the parent div's scrollTop`to the target element's offsetTop. This method allows smooth navigation within a scrollable container. Below are the methods to scroll to an element inside a Div using JavaScript:Table of Content Using scrollIntoVi
3 min read
How to Locate Elements using Selenium Python?
Selenium: is an open-source tool that automates web browsers. It provides a single interface that lets you write test scripts in programming languages like Ruby, Java, NodeJS, PHP, Perl, Python, and C#, among others. I personally prefer Python as itâs very easy to write code in python. A browser-dri
3 min read