Open In App

How to scroll up and down in selenium by defining pixels using javascriptExecutor ?

Improve
Improve
Like Article
Like
Save
Share
Report

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.

  1. 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
  2. Selenium: Download the Selenium latest stable version here
  3. 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:

  1. Open Eclipse and click File > New > Java Project.
  2. Provide the Project Name and click on the Finish button.
  3. In the Package Explorer (left-hand side of the window) select the project which you have created.
  4. Right-click on the src folder, select New > Class from the submenu. Provide the Class name and click on the Finish button.
  5. Add the Selenium Jars, for reference check this article https://www.geeksforgeeks.org/how-to-open-chrome-browser-using-selenium-in-java/
  6. Write the program and save it.
  7. 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.



Last Updated : 05 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads