How to Scroll Down and Up in Selenium WebDriver

How to Scroll Down and Up a page in Selenium WebDriver?

Table of Contents

What is a Scrollbar?

Selenium WebDriver is a powerful tool widely used in automation testing to interact with web elements, and one common interaction technique it facilitates is the use of a Scrollbar. In web applications, a scrollbar is an essential UI component that enables users to navigate through content that exceeds the dimensions of the visible screen area.

Whether the content comprises text, images, or any other web elements, the scrollbar allows for smooth scrolling in either horizontal or vertical directions. This feature is particularly important when handling long web pages or large data sets, as it enhances user experience by making the entire content accessible without overwhelming the visible screen space.


In the context of a Selenium WebDriver course and automation, handling scrollbars becomes crucial, especially when testing pages with dynamically loaded content or endless scrolling. WebDriver provides several methods to automate scrolling actions, allowing testers to simulate user interactions such as scrolling to specific elements or reaching the bottom of the page. This skill is essential for automating tests on content-rich web applications where scrolling behaviors impact user experience.

These actions are often necessary to ensure that all elements are loaded and displayed correctly, and they enable automated testing of various scrolling scenarios, like infinite scroll, sticky elements, or lazy loading.

The Selenium WebDriver can interact with the scrollbar using JavaScript Executor, a feature within Selenium that allows the execution of JavaScript commands directly in the browser.

For instance, commands can be used to scroll to a particular pixel, to the end of a page, or until a specific element is in view. This approach is helpful in overcoming limitations of traditional automation methods when dealing with scrollable content, ensuring that tests can access elements hidden outside the viewport.

It helps to view the content on a screen by scrolling the web page up, down, left, or right.

There are mainly two types of the scrollbar: horizontal and vertical scroll bars.

How to Scroll Up and Down in Selenium WebDriver

How to Scroll Down the Web Page by Pixel in Selenium?

To scroll down a web page by pixel, we will use the scrollBy() method. This method will scroll the web page in the window by a specified number of pixels left or right and up or down. The syntax for the scrollBy() method of Javascript is given below:

window.scrollBy(int x-pixels, int y-pixels);

This method is called by using the window object by passing the number of pixels as parameter values to the method.

The browser will automatically creates the object of the window. The window object denotes the browser’s window. All the global variables, functions/methods, and objects of Javascript can be called by using this window object because they are members of the window object.

In Selenium WebDriver, the x-pixels parameter defines the number of horizontal pixels to scroll along the x-axis, allowing access to off-screen elements that may extend beyond the viewport’s width. This is particularly useful for testing wide layouts, tables, or carousels where content isn’t fully visible on smaller screens.

By adjusting the x-pixels value, testers can simulate precise horizontal scrolling, ensuring all elements are accessible and displayed correctly across various screen sizes.

If the passed number is positive, the window will move to the left and movesto the right if the number passed is negative.

y-pixels is a numeric value that represents the vertical number of pixels. y-pixels represents number at the y-axis. If the number passed is positive, then the window scrolls down, whereas, if it is negative, the window scrolls up.

Let us automate a simple scenario in which we will scroll the web page down by 1000 pixels vertically.

Scenario to Automate:

a. Launch the Firefox web browser.

b. Open the website “https//www.yahoo.com.”

c. Scroll the home page down by 1000 pixels vertical.

Below is the code:

package scrollUpandDown; 
import org.openqa.selenium.JavascriptExecutor; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
public class VerticalScrollDown 
{ 
public static void main(String[] args) 
{ 
   WebDriver driver = new FirefoxDriver(); 
   driver.manage().window().maximize(); 
   String url = "https://www.yahoo.com"; 
   driver.get(url); 
   JavascriptExecutor js = (JavascriptExecutor)driver; 
// Call scrollBy() method of Javascript using the window object by passing number of pixels as parameter values. 
// Call executeScript() method of JavascriptExecutor interface by passing window.scrollBy() as a parameter value.
   js.executeScript("window.scrollBy(0,1000)");
  } 
}

How to Scroll Down Web Page by the visibility of Element?

Let us take a scenario where we will scroll down a web page by the visibility of an element.

Scenario need to Automate:

1. Launch the Firefox browser.

2. Open the given URL: “https://selenium08.blogspot.com/2020/02/vertical-scroll.html” in the Firefox browser.

3. Now, scroll the web page till the mentioned element is visible on the current page.

To scroll down a web page until the mentioned element’s visibility, we will use a scrollIntoView() method. This method is used to scroll the web page or browser window until the mentioned element is visible on the screen. 

The syntax for the scrollIntoView() method is as follows:

Syntax:

element.scrollIntoView(alignTo)

This method accepts an optional boolean value and does not return anything. A boolean value means the type of alignment.

Below is the code:

package scrollUpandDown; 
import org.openqa.selenium.JavascriptExecutor; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
public class ScrollByVisibleElement 
{ 
public static void main(String[] args) 
{  
   WebDriver driver = new FirefoxDriver(); 
   driver.manage().window().maximize(); 
   String URL1 = "https://selenium08.blogspot.com/2020/02/vertical-scroll.html"; 
   driver.get(URL1); 
   WebElement element1 = driver.findElement(By.xpath("//a[text() = 'Health']")); 
   JavascriptExecutor js = (JavascriptExecutor)driver; 
    js.executeScript("arguments[0].scrollIntoView();", element1);
   } 
}

In the above statement “js.executeScript(“arguments[0].scrollIntoView()”, element );”, arguments[0] denotes first index of page starting from 0 whereas,  ” element ” is the locator on the web page.

Now we will run the above test script and observe the output on the web page whether the web page will scroll down until the visibility of the element or not. The output of the above is shown below.

How to Scroll Down Web Page by visibility of Element?

How to Scroll Down at bottom of a Web page?

Let us take the same URL as and we will scroll down till the bottom of the page. The scrollTo() method scrolls until the end of the page. This method will scroll the web page to the specified coordinates. The syntax for the scrollTo() method is given below:

Syntax:

window.scrollTo(int x, int y)

This method will accept two integer values as parameters in pixels and will return nothing.

For instance: 

Scroll the web page to position 400 horizontally and 500 vertically:

window.scrollTo(400, 500);

Below is the code:

package scrollUpandDown; 
import org.openqa.selenium.JavascriptExecutor; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
public class ScrollByPage 
{ 
public static void main(String[] args) 
{ 
   WebDriver driver = new FirefoxDriver(); 
   driver.manage().window().maximize(); 
   String URL = "https://selenium08.blogspot.com/"; driver.get(URL); 
   JavascriptExecutor js = (JavascriptExecutor) driver; 
   js.executeScript("window.scrollTo(1, document.body.scrollHeight)");
  }
 }

In the above code, we will first launch the given URL in the Firefox browser and then scroll the web page horizontally until the mentioned element is visible on the current screen.

Now we will run the above code and observe the output. When you execute the above script, you will get the following output as shown in the below screenshot.

How to scroll web page horizontal

Conclusion

In conclusion, scrolling up and down a webpage in Selenium WebDriver is a fundamental skill for automated testing, allowing testers to interact with page elements that may not be visible initially. By using JavaScriptExecutor, Actions class, or keys like PageDown and PageUp, testers can effectively navigate through dynamic or lengthy pages, ensuring that all elements are accessible and testable.

These methods provide flexibility and control, enhancing the overall testing process and enabling more comprehensive coverage of user interactions on a webpage.

Mastering scrolling techniques in Selenium WebDriver not only increases testing efficiency but also contributes to creating robust and reliable automation scripts. As web applications become more complex, the ability to seamlessly interact with all parts of a page is essential for accurate testing.

Incorporating these techniques into your Selenium automation testing framework ensures that your tests can handle various page layouts and dynamic content, making your automation suite more versatile and effective in identifying issues across the entire user experience.

By mastering scrolling interactions, your Selenium tests gain the ability to seamlessly navigate through content-rich applications, ensuring that every element, even those initially out of view, is accessible and functions as expected.

This skill is essential for verifying user interactions in complex or dynamically loaded environments, where content shifts and expands as users scroll. Whether you’re testing infinite scrolls, pop-ups, or lengthy forms, effective scrolling techniques enable you to interact with every component, enhancing the reliability of your tests and providing a thorough assessment of the user experience across diverse page layouts.

Call to Action

Ready to elevate your Selenium testing skills? Mastering page navigation techniques, like scrolling up and down in Selenium WebDriver, is just one of many ways to build more effective and comprehensive test scripts. Practice these methods in real-world scenarios to gain confidence in handling dynamic web pages, and ensure your automation testing covers every element with ease.

If you’re eager to dive deeper into Selenium WebDriver and enhance your test automation toolkit, explore advanced courses and resources available for Selenium and other testing frameworks. Enhance your automation expertise by mastering the latest testing techniques, which are essential in today’s fast-evolving tech landscape. By consistently expanding your skills, you position yourself as a valuable asset in QA automation, capable of handling sophisticated testing demands and ensuring high-quality software delivery.

Embrace opportunities to deepen your understanding of advanced tools, frameworks, and practices; doing so not only elevates your professional skill set but also establishes you as a sought-after expert in the competitive world of test automation, ready to tackle any challenge with confidence.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class