Open In App

How to scroll down to bottom of page in selenium using JavaScriptExecutor

Last Updated : 05 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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#, etc. In this article, we will be working with Java and selenium to automate the scrolling of the webpage.

Scrolling is an important feature for any webpage, To scroll down the web pages we use the JavaScriptExecutor. The javascriptexecutor is an interface that enables to running of JavaScript methods from Selenium scripts.

Installation: To work with JavaScriptExecutor, we needed  Java Installed in our system and Selenium, a web driver 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:

  • JavaScriptExecutor 
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,document.body.scrollHeight)", "");
  • Scroll Down to the bottom of the page:
window.scrollBy(0,document.body.scrollHeight)

Selenium runs the commands in Javascript with the execute_script() method. For scrolling down to the bottom of the page, we have to pass (0, document.body.scrollHeight) as parameters to the method scrollBy().

let us consider a test case in which we will try to automate the following scenarios in the Edge browser.

  • Launch the Edge browser
  • Maximize the browser.
  • Open URL: https://www.geeksforgeeks.org/
  • Scroll up to the bottom of the page using the JavaScriptExecutor

Step 1: Set a system property “webdriver.edge.driver” to the path of your EdgeDriver.exe file and instantiate an EdgeDriver class.

Javascript




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 
    }
}


Step 2: Maximize the window: driver.manage().window().maximize()  and open the geeksforgeeks site.

Javascript




driver.manage().window().maximize();
// Launch Website 


Step 3: Initiate the javascriptexecutor and scroll method to scroll to the bottom of the page.

Javascript




JavascriptExecutor js = (JavascriptExecutor) driver;
//Scroll down till the bottom of the page
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");


Example: Program to  scroll down to the bottom of the webpage using Selenium:

Java




import org.openqa.selenium.WebDriver;
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 till the bottom of the page
        js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
 
    }
}


Output:

 

The above code starts the Edge browser and navigates to the geeksforgeeks page, once the webpage loads, Selenium automatically fetches the maximum height of the webpage from the Document Object Model, and then the scrollBy() method scrolls down to the bottom.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads