Open In App

Selenium Scrolling a Web Page using Java

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

An open-source framework that is used for automating web applications is known as Selenium. Selenium handles various operations, like opening of website, clicking buttons, scrolling the webpage, etc. In this article, we have defined the numerous ways to scroll a webpage using Selenium in Java.

Selenium Scrolling a Web Page using Java

Now, we will discuss the various ways to scroll the webpage using Selenium webdriver in Java.

Scroll Down to the Page’s Bottom

If the user knows the element he is finding for further actions is at the bottom of the page, then this is the best approach. In this approach, we have opened the Geeks For Geeks website (link) and then scrolled to the bottom of the page.

// Importing the Selenium libraries
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

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;

        // Scroll to bottom of page
        js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
    }
}

Output:

scroll-down-page-using-selenium-java

Scroll Based on the Visibility of the Web Element on the Page

Whenever the user wants to scroll the page till the driver finds the position of the element on the webpage, it is the best approach. In this approach, we opened the Geeks For Geeks website (link) and then scrolled till the webdriver found the element containing the text 'Problem of the day' using the contains and findElement function.

Output:

scroll-based-on-the-webelement-using-java

Scroll a Webpage Both Horizontally and Vertically

This is the best approach if the user knows he needs to scroll the webpage horizontally as well as vertically to find the web element. In this approach, we have opened the Geeks For Geeks website (link) and then scrolled the webpage both horizontally and vertically.

  • Horizontal Scroll: Here we have scrolled the webpage horizontally by 50 pixels.
  • Vertical Scroll: Here we have scrolled the webpage horizontally by 1500 pixels.

Output:

scroll-page-using-selenium-horizontally-vertically-using-java

Scroll a Webpage with Infinite Scrolling

When the webpage is too large and the user wants to scroll a webpage infinitely till he reaches the end of the webpage or at the specific position he wants, then he can use this approach. In this approach, we opened the Geeks For Geeks website (link) and then scrolled infinitely till he reached the end of the webpage using a while loop and break feature.

Output:

infinite-scroll-using-java-selenium

Scroll to the Top of the Page

Sometimes the webpage is too large and the user has scrolled to a specific position, and then if the user has to scroll back to the top of the page, this is the best approach. In this approach, we have opened the Geeks For Geeks website (link) and then scrolled to the top of the page.

Output:

scroll-top-page-using-selenium-using-java

Scroll Down a Page by Specified Pixels

This is the best approach if the user knows the exact dimensions of the webpage, where the web element is present. In this approach, we have opened the Geeks For Geeks website (link) and then scrolled the webpage both horizontally and vertically.

Output:

scroll-down-page-specified-pixels-using-java

Conclusion

In conclusion, scrolling a webpage using Selenium WebDriver in Java can happen in various ways, from specific pixels, to the bottom of the page, to the top of the page, to infinite scrolling, etc. I hope after reading the above article, you will be able to do all types of scrolling using Selenium in Java.


Next Article
Article Tags :

Similar Reads