Monday, August 8, 2011

Scrolling on pages using Selenium

Somebody recently asked me on how to scroll a page which is dynamically loaded. Something like the Facebook page where the updates gets loaded as you scroll down the page. As I never had such a requirement , I didn't knew a solution to it. 
While searching I found people suggesting the "selenium.focus" function to be used as a solution, but that does not solve the problem I am talking about.  The "selenium.focus" should be used when the element you are looking for gets loaded when the page gets loaded and not when you scroll through the page.
After lot of searching I was unable to get a proper answer to my problem and finally got an idea for implementing it. Its just a 4 to 5 lines of code that can be used in your code for scrolling on the page.
Following is the code:


The above code uses the JavaScript method "scrollBy" to scroll on the page. The for loop runs for 60 sec  and calls the scrollBy function every second. This makes the selenium to scroll on the page.
If you have a test where you need to scroll on the page and check whether an element is loaded dynamically or not you can put a isElementPresent like function after the "driver.executeScript" to check visibility of your element.
Following is a test method written in webdriver that you can use to test the above function on the Facebook page:


The following code is written for Selenium-1.0 users:

17 comments :

Arun said...

hi
thanks for the post

i want to scroll the list box in a webpage

ex:http://www.obout.com/ListBox/aspnet_ondemand_virtual_scroll.aspx

how to scroll the Asp.nte Listbox scroll bar?

Varun Menon said...

@Arun - Its a good problem to solve. But sorry I havent worked on such kind of thing. But by looking at it you can go try following approaches.
- Use actions class implementation of selenium. Select an element and then use the keyboard down button to load the whole list.
- Hover your mouse on the list box and then call the scroll option.

Aneta said...

Thanks , this code helped in android, for android selenium testing, this would be a line to add if you need to scroll across the android browser kit

((AndroidWebDriver) driver).executeScript("window.scrollBy(0,200)", "");

Varun Menon said...

Thanks @Aneta

Ranjana said...

Thaks Varun ,this post has helped me :)
Ranjana

gS said...

How about scrolling a table horizontaly inside a webpage ?how will we do it ?

Varun Menon said...

@gS - No idea on how to scroll on a particular area.

Unknown said...

Varun,

i am stuck with below scenario, can you plz help

I have 2 frames in the web page, scroll bar is present in the left frame of the page, when i try dng ur method as mentioned above it does notw ork , can u suggest me anyother work around.

i need to scroll down to click on one link in the left pane

regards,
keshav

Varun Menon said...

Why do you need to scroll?
Webdriver by default automatically scroll to the said element when you need to take a action on it.
Scroll may be required when the element is loaded on scroll down as in the case of facebook and many other apps.

rajan said...

Really your solution helps me varun.
Thank You :)

sai krishna said...

Varun

In my case when i scroll the data gets loaded. But the scroll is not the firefox window scroll , its a scroll within the webpage as keshav is mentioned.

In that case how can i focus on that scroll bar and scroll it .??

vibhu said...

Hi All,
I tried this code but this does not works for scrolling down in gmail.. could you plz help!!

Regards

Unknown said...

Hi Varun ,
I have one doubt. please help me

driver.executeScript("window.scrollBy(0,200)", "");

please explain this line of code.
What is 0 and 200 ? its time or anything else ? And inside double coded u have not written anything

Varun Menon said...

0 & 200 is the x and y coordinates. Also the second argument of executeScript method contains extra arguments that have to be given to the script. Please check the following links:

http://www.w3schools.com/jsref/met_win_scrollby.asp

http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/JavascriptExecutor.html#executeScript(java.lang.String, java.lang.Object...)

Nisha said...

Hi Varun,
i have one table containing scrollbar in the web page .I have to click the last element in the table.How will i do that because the element is not in focus.

Unknown said...

Hi, could you please also tell us how to scroll inside a web table?

Aditi Rajpurohit said...

Hey Arpitha, Varun and others who want to scroll a table in a webpage.

We can do this by

1. locating the table in a webelement

like -> WebElement table = driver.findElement(By xpath or css path of the table);

2. to scroll the table vertically or horizontally, you have to locate a hidden row or column of the table

do this- > table.findElement(xpath or css path of the hidden element);

Hope this helped :)

Post a Comment