Table of contents
1.
Introduction
2.
Defining the Scroll Function
3.
Scrolling Down on a Selenium Web page
4.
Scrolling Up on a Selenium Web page
5.
Scrolling down to an element until it is visible
6.
Scrolling down to the bottom of the webpage
7.
Frequently Asked Questions
8.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Scrolling a web page

Author Parth Jain
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Even with the availability of media queries and other modern responsive technologies, it is impossible to adjust all of a website's content inside a single view. Hence a scroll bar is used to guide us through both horizontal and vertical directions. 
Selenium can manipulate the DOM (Document Object Model) and hence does not usually need a scroll to implement directional actions. Still, there can be cases where we need the assistance of a scroll function and, therefore, will learn how to implement scrolling in a Selenium application.

Defining the Scroll Function

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,250)", "");

Here, the scrollBy function takes parameters a and b that represent the horizontal and vertical values.  

Scrolling Down on a Selenium Web page

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class HandleScroll 
{

 @Test
 public void scrollingDown()
         {
          System.setProperty("webdriver.gecko.driver","D://Selenium    Environment//Drivers//geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
            driver.navigate().to("URL");

            //to perform Scroll Down in our application
            JavascriptExecutor js = (JavascriptExecutor) driver;
            js.executeScript("window.scrollBy(0,500)", "");
         }
}

Output: A Firefox browser window is launched, accessing the specified website URL. Upon loading, the browser window is vertically scrolled down by 500 pixels.

Scrolling Up on a Selenium Web page

Similar to scrolling down we change the parameters of the scrollBy function to achieve an upward scroll

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class HandleScroll 
{

 @Test
 public void scrollingUp()
         {
          System.setProperty("webdriver.gecko.driver","D://Selenium    Environment//Drivers//geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
            driver.navigate().to("URL");

            //to perform Scroll Up in our application
            JavascriptExecutor js = (JavascriptExecutor) driver;
            js.executeScript("window.scrollBy(0,-500)", "");
         }
}

Output: A Firefox browser window is launched, accessing the specified website URL. Upon loading, the browser window is vertically scrolled upwards by 500 pixels.

Scrolling down to an element until it is visible

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class ScrollToSelectedElement {

    WebDriver driver;
    @Test
    public void ByVisibleElement() {
        System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe"); 
        WebDriver driver = new FirefoxDriver();
        JavascriptExecutor js = (JavascriptExecutor) driver;

        //Launching the application 
        driver.get("A website’s URL");

        //Storing the text element in a variable called "component"         
        WebElement component = driver.findElement(By.linkText("This is a sample text"));

        // Scrolling down till the element is found 
        js.executeScript("arguments[0].scrollIntoView();", component);
    }
}

Output: A Firefox browser window is launched, accessing the specified website URL. Upon loading, the browser window automatically navigates to where the component text is located.

Scrolling down to the bottom of the webpage

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class HandleScroll

{
  @Test
  public void scrollingToEnd()
  {
   System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe");
   WebDriver driver = new FirefoxDriver();
   driver.navigate().to("URL"); 

   JavascriptExecutor js = (JavascriptExecutor) driver;
   js.executeScript("window.scrollBy(0,document.body.scrollHeight)");

  }
}

Output: A Firefox browser window is launched, accessing the specified website URL. Upon loading, the browser window automatically navigates to the bottom of the webpage.

Frequently Asked Questions

  1. How does Selenium handle scrolling?
    The scrollBy() method involves two parameters, x, and y, representing the horizontal and vertical pixel values.
  2. What is Selenium Grid?
    Selenium Grid is an intelligent proxy server that makes it easy to run tests in parallel on multiple machines. This is accomplished by routing commands to remote web browser instances, where one server acts as the hub. 
  3. How do you scroll down a webpage in Selenium?
    To scroll the web page, you can use JavascriptExecutor. Create an object for JavascriptExecutor and call the web driver. Then execute the script with a scrollTo function, and using that, you can either scroll to a particular section or the bottom of your page.
  4. What is scroll into view?
    The scrollIntoView() is a function used to scroll an element into the visible area of the browser window.
  5. What does scroll down mean?
    Scroll down is an action performed on a mobile or a desktop platform to move down to a particular section of a webpage.

Key Takeaways

In this article, we have extensively discussed Scrolling a webpage in Selenium.If you are Preparing for interview and don't know where to start, we have got you covered, check out our expert curated courses on our website, You can also check out Coding Ninjas Studio to practice frequently asked interview problems. We hope that this blog has helped you enhance your knowledge regarding Selenium and if you would like to learn more, check out our articles.  Do upvote our blog to help other ninjas grow. Happy Coding!"

Live masterclass