Scroll on a page or list view in selenium using javaScript

There are some situations in which we want to scroll to a particular element or scroll up or down across the browser, and also we can scroll inside a dropdown or list box to get all the available options.

Selenium do not have it’s own methods to perform this action, so we can use javascript to play on scrollbars.

 

Let’s see some of the ways:

 

To scroll on a web page –

Another way –

 

Note

These 2 below lines will scroll till the “Automation Tools” list box section on browser screen

element = (WebElement) js.executeScript("return document.getElementById('tools');");
js.executeScript("arguments[0].scrollIntoView(true);",element);

To scroll inside a dropdown / list box –

Above ways to scroll on a browser screen, but there are situations in which we need to scroll on a dropdown list, then we need to 1st locate the dropdown list and then perform scrolling

Example – on our demo screen, let’s say we want to scroll inside the Automation tools list box section, then you can use below code

js.executeScript("elem = document.querySelector('#tools'); elem.scrollTop = elem.scrollTop + 100");
document.querySelector() locates the list box and scrolls 100px down, you can change the value with your need.

 

Complete code block

 

Content posted is based on learning or working experience,
Share the post with others if find useful,
Any queries?, leave comments or discuss on our facebook:qavalidation.com, Thanks!

1 Response

  1. June 12, 2016

    […] How to scroll on a page or to view an element […]

Leave a Reply

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