How to scroll up and down in selenium by defining pixels using javascriptExecutor ?
Last Updated :
25 Jul, 2024
In this article let's see how to scroll up and down using the javascriptexecutor.
A scroll is a JavaScript method, the JavaScriptExecutor provides the interface that enables to use of the JavaScript methods from selenium scripts. Hence to scroll up or down in selenium JavaScriptExecutor is needed.
Installation: To work with JavaScriptExecutor, we needed Java Installed in our system and Selenium, a webdriver for the browser, also an IDE for code editor.
- Eclipse IDE: Before downloading also make sure that your device has Java JDK. If you don’t have, install Java refers to this: How to Download and Install Java for 64-bit machine?. And install Eclipse IDE by referring to this article Eclipse IDE for Java Developers
- Selenium: Download the Selenium latest stable version here
- Web Driver: Download the Microsoft Edge webdriver according to your version here
Syntax:
window.scrollBy(xnum,ynum);
Parameters:
- Xnum: is the number required for how many pixels to scroll by, along the x-axis (horizontal). Positive values will scroll to the right, while negative values will scroll to the left.
- Ynum: is the number required for how many pixels to scroll by, along the y-axis (vertical). Positive values will scroll down, while negative values scroll up.
Scroll function:
javascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(xnum,ynum)", "");
Example:
- To scroll Up: If a user wants to scroll Up, they just need to modify the pixel value of the second parameter to a negative value (eg.350).
- To scroll Down: If a user wants to scroll Down, they just need to modify the pixel value of the second parameter to a positive value (eg.350).
Program to Scroll Web page Down using Selenium WebDriver:
Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
public class geeksforgeeks {
public static void main(String args[]) {
System.setProperty("webdriver.edge.driver",
"C:\\Users\\ADMIN\\Documents\\Selenium\\msedgedriver.exe");
// Instantiate a EdgeDriver class.
WebDriver driver=new EdgeDriver();
// Maximize the browser
driver.manage().window().maximize();
// Launch Website
driver.get("https://www.geeksforgeeks.org/");
JavascriptExecutor js = (JavascriptExecutor) driver;
// Scroll Down by 350 pixels
js.executeScript("window.scrollBy(0,350)", "");
}
}
Steps to run the code:
- Open Eclipse and click File > New > Java Project.
- Provide the Project Name and click on the Finish button.
- In the Package Explorer (left-hand side of the window) select the project which you have created.
- Right-click on the src folder, select New > Class from the submenu. Provide the Class name and click on the Finish button.
- Add the Selenium Jars, for reference check this article https://www.geeksforgeeks.org/how-to-open-chrome-browser-using-selenium-in-java/
- Write the program and save it.
- Now, press Ctrl+F11 or click on the Run menu and select Run or click on the Run button.
Output: The code initializes the GeeksforGeeks for Edge browser. Then the Edge browser is launched, and it navigates to the specified website URL. Once the website loads, the browser window is vertically scrolled down by 350 pixels.
Similar Reads
How to scroll down to bottom of page in selenium using JavaScriptExecutor Selenium is an open-source popular web-based automation tool. The major advantage of using selenium is, it supports all browsers like Google Chrome, Microsoft Edge, Mozilla Firefox, and Safari, works on all major OS and its scripts are written in various languages i.e Java, Python, JavaScript, C#, e
3 min read
How to Get and Set Scroll Position of an Element using JavaScript ? In this article, we will learn how to get and set the scroll position of an HTML element using JavaScript.Approach: We will be using the HTML DOM querySelector() and addEventListener() methods and the HTML DOM innerHTML, scrollTop and scrollLeft properties.We create an HTML div element with an id of
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 get the height of scroll bar using JavaScript ? To get the height of scroll bar we could use different approaches. In this article, we are given an HTML document and the task is to get the height of the scrollbar using JavaScript. Following are the different approaches to solving this problem which are discussed below: Table of Content Using Cont
3 min read
How to set smooth scroll after clicking the link using JavaScript ? Smooth scroll provides a seamless user experience as it provides an effective way to enhance website navigation. By implementing smooth scroll functionality, one can ensure that users transition fluidly between different sections of your webpage when they click on internal links.There are several me
4 min read
How to scroll the page up or down using anchor element in jQuery ? The problem is to include a slide effect whenever we click a local anchor and we want to scroll up or down the page accordingly. Earlier we can do it natively by using CSS property. Syntax: a { scroll-behavior: smooth; } Now with the help of jQuery, we can do it by using the following two methods: j
2 min read