testsigma
left-mobile-bg

How To Scroll a Page Using Selenium WebDriver with Java?

May 29, 2024
Shreshtha Gokhru
right-mobile-bg
How To Scroll a Page Using Selenium WebDriver with Java?
image

Start automating your tests 10X Faster in Simple English with Testsigma

Try for free

Automating an application can be performed in various ways with a variety of tools. Because it is free and simple to use, Selenium WebDriver is the most popular of those. Selenium WebDriver gives users a lot of flexibility to write their test in different programming language and allow the user to execute their test on different browser like Firefox, Chrome, Edge, Safari, etc

Let’s understand why scrolling is important. 

Scrolling is one of the most important features of any application. You will not find any website which does not have scrolling capability. It’s almost impossible to display all the content in a single frame, users need to scroll down or up to get all the content of the application. 

QAs are responsible to create robust tests and deliver bug-free application. They must specifically make sure that every UI element is operating as intended. 

Selenium WebDriver directly interacts with the DOM, and several operations can be carried out without the need to scroll. However, there are rare instances where specific web elements are only accessible after the user scrolls to the element. In these situations, automating the scrolling process is required.

This article will help you to understand horizontal and vertical scroll operations with Selenium WebDriver.

Starting your journey with Selenium WebDriver? Check the article on Selenium WebDriver.

In Selenium, scrolling can be handled by using JavaScriptexecutor. JavaScriptexecutor is an interface that is used to execute JavaScript through selenium webdriver. The JavaScript Executor is a powerful feature of Selenium WebDriver that allows you to execute JavaScript code directly in the browser.

JavaScriptexecutor provides two methods:

  1. ExecuteScript
  2. ExecutrAsyncScript

Syntax

ScrollBy is a JavaScriptexecutor method that accepts two parameters x and y, which defines horizontal and vertical axes.

Now let’s discuss the Scroll operation in different scenarios:

  1. How do I scroll down to the page’s bottom using Selenium?
  2. How do I scroll based on the visibility of the Web element on the page in Selenium?
  3. How does Selenium scroll a webpage both horizontally and vertically?
  4. How do you scroll a webpage with infinite scrolling using Selenium?
  5. How do you Scroll to the top of the page in Selenium?
  6. How do you scroll down in a page by specified pixels in Selenium?

How do I scroll down to the page’s bottom using Selenium?

In Selenium, you can use the JavaScript Executor to scroll down to the bottom of a web page. You can use the “scrollTo” method to scroll to the bottom of the page. The following code snippet shows an example of how to scroll down to the bottom of a web page using Selenium in Java:

In the above example, the JavaScript Executor is first cast to a JavascriptExecutor object and then the “executeScript” method is used to execute a JavaScript code. The JavaScript code passed to the “executeScript” method is “window.scrollTo(0, document.body.scrollHeight)”, which scrolls the page to the bottom.

Code snippet:

Above code snippet will help you to scroll your page to the bottom of the page.

How do I scroll based on the visibility of the Web element on the page in Selenium?

Before you perform scroll operations in selenium, it is necessary that the corresponding web element is present under DOM.

ScrollIntoView() method will help you to scroll to the desired element.

The scrollIntoView() method is a JavaScript method that can be used to scroll an HTML element into the viewport. When called on an element, it will scroll the element’s parent container so that the element becomes visible in the viewport.

Code snippet:

How does Selenium scroll a webpage both horizontally and vertically?

In Selenium, you can use the JavaScript Executor to scroll a web page both horizontally and vertically. However, the JavaScript Executor does not have the method to scroll a web page both horizontally and vertically. It has the method “scrollBy” and “scrollTo” which can scroll the page horizontally or vertically.

In order to scroll the page both horizontally and vertically, you need to use the “scrollBy” method twice in a row, once for horizontal scrolling and once for vertical scrolling. Here is an example of how to use the “scrollBy” method to scroll a web page both horizontally and vertically in Selenium: 

Code snippet:

For Horizontal Scroll


For Vertical Scroll


How do you scroll a webpage with infinite scrolling using Selenium?

In today’s world, most applications implement infinite scrolling. Since users do not have to do the time-consuming operation of pressing the “Previous” and “Next” buttons, pages with endless scrolling may see improved engagement.

You can visit https://the-internet.herokuapp.com/infinite_scroll to look at infinite scrolling. 

Code snippet

How do you Scroll to the top of the page in Selenium?

In the above article, we have seen scrolling down or scrolling horizontally but scrolling to the top of the page is one of the widely used operations on pages using the JavascriptExecutor interface. 

To demonstrate this, we first need to scroll down to the page using selenium and then scroll up to the page.

There are various ways to scroll to the top of the page. 

Method 1: 

How do you scroll down a page by specified pixels in Selenium?

With the help of JavaScriptExecutor, you can scroll to a particular point by specifying the exact pixel or coordinates of the object. This can be achieved by using the ‘ScrollBy’ function.in JavaScript. 

Code Snippet:

Scroll A Page With Low/No-code: Testsigma

Alternatively, there is a quick and efficient way to automate this test scenario.  You can leverage Testsigma’s low-code techniques and create robust test steps to automate scrolling in less than a minute. 

Let’s take an example: Consider a user who wants to look up available flights from A to B, where they scroll through various relevant results to find the ultimate one. This can be done in a few steps with less time and effort, yet efficiently using codeless testing features supported by Testsigma, like NLPs, Record & Playback, and Data-driven testing. 

Look at the screenshot below, where scrolling is automated using a Scroll NLP.

Testsigma


Check out the comprehensive comparison of Selenium vs Testsigma and how they impact the test automation for your business in terms of speed, efficiency, cost, test ROI, and more.

Stay up to date with such resourceful blogs,

subscribe to our monthly newsletter

Join our QA Community

Conclusion

In the above article, we have seen different ways of scrolling on a web page using Selenium with Java. One of the frequently performed tasks when it comes to Selenium automation testing is automating the page scroll action utilizing the JavaScriptExecutor interface. We have seen sample code for scrolling down, scrolling up, scrolling horizontally and vertically, etc. Do share your thoughts on how you have implemented scrolling a web page in your scripts. 

Do share this blog with your friends and colleagues. 

Happy testing 😊

Frequently Asked Question

How do I scroll down using sendKeys in selenium?

In Selenium, you can use the sendKeys() method to simulate key presses, including the “Page Down” key. To scroll down using sendKeys(), you can use the following code:

This will simulate a “Page Down” key press on the element, which will scroll the element down.

How do I scroll down a page using Java by specified pixels in Selenium?

In Selenium using Java, you can use the JavascriptExecutor class to execute JavaScript code that can scroll the scroll bar on a page. Here is an example of how you can scroll the scroll bar down by a certain number of pixels:

This will scroll down the web page by 1000 pixels. You can adjust the number of pixels to scroll by as needed.

Testsigma Author - Shreshtha Gokhru

Shreshtha Gokhru

An astute professional with over 8+ years of experience in Software testing, Agile methodologies, and product sales. I am also keen to invest in the Stock market and Mutual funds. Apart from that, I like travelling and playing cricket.

image

Start automating your tests 10X Faster in Simple English with Testsigma

Try for free
imageimage
Subscribe to get all our latest blogs, updates delivered directly to your inbox.

By submitting the form, you would be accepting the Privacy Policy.

RELATED BLOGS


Scriptless Test Automation | What , Why it Matters & Examples
KIRUTHIKA DEVARAJ
TEST AUTOMATION
Top 6 Game Testing Tools You Need to Know
RAUNAK JAIN
TEST AUTOMATION
POC in Testing | What , Why it Matters & How to Perform ?
VIJAYARAGHAVAN VASUDEVAN
AUTOMATION TESTINGTEST AUTOMATION