How JavascriptExecutor helps in scrolling webpage in Selenium?

Category: Selenium WebDriver 64 0

There are some test failure situations which terminates the test execution by sending some Selenium exceptions like NoSuchElementException and ElementNotVisibileException. When we go to the root cause of failure we found that Element was correct and it was even visible in DOM when checked with isDisplayed() method, but, the test failed because the position of the element was at the bottom of the web page. If we manually scroll to the bottom where the element is available then test gets passed. We found the solution and we concluded to insert two additional lines in the code to handle the scrolling webpage part. Hence, we identified that JavascriptExecutor helps in scrolling webpage in Selenium WebDriver.

So, it was a bingo for us! 🙂

Those who are naïve to this scenario let me tell you from the basics of scrolling scenario.

Do you know?

How to handle error message using Selenium WebDriver?

scrolling webpage in selenium

What is a scrollbar?

A scrollbar is a business requirement which appears when any applications or entire web page fails to fit at a single time in a given resolution of the screen.

This definition is the best suitable from the reference point of view of a business analyst. Let me explain to you what a non-analyst or non-techy defines scrollbar.

A scrollbar is a long strip which is placed at the extreme right edge of the web page and the extreme bottom edge of the same web page as well. The strip or scrollbar which is placed at the extreme right edge is used to scroll the page up and down when entire content is not visible at once; whereas, the strip which is placed at the extreme bottom edge of the webpage allows moving right and left to mark the visibility of the content.

You saw how two kinds of people define the scrollbar. Anyway, our main goal is to satisfy the need of a Business analyst and a beta user as well.

What are the constraints in scrolling webpage with Selenium?

We encounter some roadblock too, which is actually not a roadblock.

Okay… Let me explain you the kind of roadblock.

When we use the scroller to see the hidden part of the content which is either at the boom or at the right side of the webpage, then DOM structure gets disrupted. Hence, we have to be very wise in scrolling webpage in Selenium using JavascriptExecutor. We can use wherever necessary only with proper wait time to load the page and the DOM hierarchy.

JavascriptExecutor helps in scrolling webpage in Selenium

Here is the sample JavascriptExecutor syntax which is used to handle any kind of adverse scenarios in Selenium.

Well, Let us stick to scrolling webpage as of now. We will discuss the in-depth study of JavascriptExecutor in a separate blog post.

Here we go…

 

Here, arg0 should have your script that you want to execute and arg1 should have an argument.

Different webpage scrolling scenarios to apply JavascriptExecutor in Selenium

You encounter mainly four types of scenarios where the requirement for scrolling webpage goes. Those scenarios are discussed in details with their sample codes.

Scenario# 1: Scrolling webpage vertically up, down and to some coordinates

This is the first scenario that we are going to discuss. In this scenario, we move the right edged scrollbar up and down and even to the extreme bottom. Let’s have a look at the script.

scrolling webpage up down

Test Case# 1: Scrolling webpage to the vertically down using JavascriptExecutor in Selenium

In this test case, we have a requirement to scroll the webpage till it reaches the bottom level. So here is the sample code.

 

We will have entire program below, but before that let me explain you the scenario. We are launching inviul.com and we are not maximizing it so that the full view of the content won’t be displayed. Once the webpage is loaded completely then we use JavascriptExecutor to scroll to the bottom. Here is the complete code snippet:

 

Test Case# 2: Scrolling webpage to vertically up position using JavascriptExecutor in Selenium

This test case is opposite to the Test Case# 1. Here, we are writing commands to scroll to the vertically up position. Here is the sample code.

 

The entire process is same. Rather, it is working in opposite direction to the first test case. The entire code snippet is below:

Test Case# 3: Scrolling webpage to coordinates using JavascriptExecutor in Selenium

In this test case, we are writing commands to scroll to some coordinates at x-position and y-position. Commands will be the same as above, instead of extreme top and bottom; we will enter some defined pixels. Let’s have a look at its command.

 

The entire program is here:

 

Scenario# 2: Scrolling webpage horizontally left and right to some coordinates

In this scenario, we are going to discuss the movement of lower edge strip to the left and right. It helps us to see the hidden content which is at the extreme right of the webpage.

scrolling webpage right left

Test Case# 1: Scrolling webpage to the horizontally right direction using JavascriptExecutor in Selenium

In this test case, we use JavascriptExecutor to scroll the webpage to the right direction. Here is the command.

 

The entire program is here-

 

Test Case# 2: Scrolling webpage to the horizontally left direction using JavascriptExecutor in Selenium

This test case is used to verify the movement of the scrollbar to the horizontally left direction and the command for the same is-

 

The program for the same test case is here-

 

Scenario# 3: Scrolling webpage till the particular web element is visible

This could be one of the most important scenarios in your Selenium Automation testing project. As discussed initially, sometime your test may fail because the web element which WebDriver is looking for may not be visible at the resolution of the screen or may be hidden at the bottom of the web page. Hence, in such scenario you need to scroll the scrollbar, using JavascriptExecutor, till the location where such element is attached to the DOM.

Let’s look at the command below:

 

Here, wb is the element where scrollbar will move. Let’s have a look at the entire program below:

Hope this tutorial will help you in scrolling webpage by using Selenium’s JavascriptExecutor library. Share your doubts in c the mment below.

Stay tuned for awesome guided.

Happy Learning. 🙂

Related Articles

Add Comment