Syntax:

Script – This is the JavaScript that needs to execute. Arguments – It is the arguments to the script. It’s optional.

Selenium Script to scroll down the page Let’s, see the scroll down a web page using the selenium webdriver with following 4 scenarios :

Scenario 1: To scroll down the web page by pixel. Scenario 2: To scroll down the web page by the visibility of the element. Scenario 3: To scroll down the web page at the bottom of the page. Scenario 4: Horizontal scroll on the web page.

Scenario 1: To scroll down the web page by pixel.

Selenium Script Script Description: In the above code first we launch the given URL in Chrome browser. Next, scroll the page by 1000 pixels through executeScript. Javascript method ScrollBy() scrolls the web page to the specific number of pixels. The syntax of ScrollBy() methods is : x-pixels is the number at x-axis, it moves to the left if number is positive and it move to the right if number is negative .y-pixels is the number at y-axis, it moves to the down if number is positive and it move to the up if number is in negative . Output analysis : Here is the output when you execute the above script .

Scenario 2: To scroll down the web page by the visibility of the element.

Selenium Script Script Description: In the above code, we first launch the given url in Chrome browser. Next, scroll the page until the mentioned element is visible on the current page. Javascript method scrollIntoView() scrolls the page until the mentioned element is in full view : “arguments[0]” means first index of page starting at 0. Where an ” Element ” is the locator on the web page.

Scenario 3: To scroll down the web page at the bottom of the page.

Selenium Script Script Description : In the above code, we first launch the given url in Chrome browser. Next, scroll till the bottom of the page. Javascript method scrollTo() scroll the till the end of the page . “document.body.scrollHeight” returns the complete height of the body i.e web page. Output analysis: Here is the output when you execute the above script.

Scenario 4: Horizontal scroll on the web page.

Selenium Script Script Description : In the above code, we first launch the given url in Chrome browser. Next, scroll the page horizontally until the mentioned element is visible on the current page. Javascript method scrollIntoView() scrolls the page until the mentioned element is in full view : Output analysis: Here is the output when you execute the above script.

Learn more about JavaScriptExecutor

What is a Scrollbar?

A Scrollbar is a lets you move around screen in horizontal or vertical direction if the current page scroll does not fit the visible area of the screen. It is used to move the window up and down. Selenium Webdriver does not require scroll to perform actions as it manipulates DOM. But in certain web pages, elements only become visible once the user have scrolled to them. In such cases scrolling may be necessary. Scroll bar is of two type : Horizontal and vertical scroll bar as shown in below screenshot.

Summary

In the above tutorial, we illustrate the scroll of the web page through different scenarios. In the first scenario, we showed the scroll down on page by pixel. In the second scenario, we showed the scroll down of page until the visible of the element. In the third scenario, we showed the scroll down of page at the bottom of the page. In the fourth scenario, illustrated the horizontal scroll on the web page.