Selenium Tutorial for Beginners

Scroll in Selenium Webdriver using Java (Horizontally | Up and Down)

Continuing on our Selenium Beginner’s series, let’s see how to Scroll in Selenium Webdriver using Java (Horizontally | Up and Down).

What is scrollbar?

A vertical or horizontal bar commonly located on the far right or bottom of a window that allows you to move the window viewing area up, down, left, or right. Most people today are familiar with scrollbars because of the need to scroll up and down in almost every Internet web page.

Scroll in Selenium Webdriver using Java

Mainly there are four scenarios to be automated with respect to Scroll in Selenium Webdriver using Java,

  1. Scroll Page up or down in Selenium WebDriver
  2. Selenium scroll down to bottom of page
  3. Selenium Webdriver scroll element into view
  4. Handle horizontal scrollbar in selenium Webdriver

How to Scroll in Selenium Webdriver using Java

To scroll in Selenium Webdriver using Java, we use the JavascriptExecutor interface which provides mechanism to execute Javascript. It provides “executescript” & “executeAsyncScript” methods, to run JavaScript in the context of the currently selected frame or window. Simply stated “Javascript can be executed within the browser with the help of JavaScript Executor.”

In order to use this interface we need to import the below package,

and then create an object using the Selenium Webdriver,

The Code

The steps followed are,

  1. Open a browser
  2. Open the website URL & maximize the window
  3. Scroll vertically downward by 250 pixels
  4. Scroll vertically upward by 250 pixels
  5. Scroll to the bottom of the Web page
  6. Scroll to a particular Web Element
  7. Scroll horizontally

Open Browser & Application

The code till,

is to open the browser and application. For reference, please visit our earlier posts in the series.

Scroll Page up or down in Selenium WebDriver

JavaScript scrollBy() method scrolls the webpage up or down by the specified number of pixels.

where:

  • xpixels – pixels to scroll along the x-axis (horizontal).
  • ypixels – #pixels to scroll along the y-axis (vertical).

Use the negative & positive values for ypixels to scroll up & down the webpage by specified pixels.

Selenium scroll down to bottom of page

To scroll to the bottom of the page, we get the height of the Body element from the DOM (Document Object Model) and then use the JavaScript method scrollTo() to scroll to the maximum height of the page,

Selenium Webdriver scroll element into view

Sometimes we need to scroll to a particular element and then perform operations on that element. For this just pass the element on which to perform the operation.

  1. Identify the Web Element from the webpage
  2. Use the JS method scrollIntoView() to scroll the page to that web element until it becomes visible

Handle horizontal scrollbar in selenium Webdriver

As with scrolling up & down, we use the xpixels argument of JavaScript scrollBy() method to scroll horizontally by the specified number of pixels.

Use the positive & negative values for xpixels to scroll right & left the webpage by specified pixels.

We hope this article gives you a basic understanding of how to scroll in Selenium Webdriver using Java (Horizontally | Up and Down).

Leave a Reply

Your email address will not be published. Required fields are marked *