You are on page 1of 5

More

Next Blog

Create Blog

Varun's Technical Blog

Aut hored

Monday , August 8, 2011

T estNG - Beginner's
Guide

Scrolling on pages using Selenium

This book explains the


various features of the
TestNG testing
framework.
It provides a step-by-step
guide that explains the
different features

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:
1
2
3
4
5
6
7

for (int second = 0;; second++) {


if(second >=60){
break;
}
driver.executeScript("window.scrollBy(0,200)", "");
Thread.sleep(1000);
}

Blog Archive

2013 (2)
2012 (4)
2011 (13)
October (3)
September (2)
August (2)
Native android app
automation
Nativedriver vs
Ro...
Scrolling on pages
using Selenium
July (6)
2010 (13)
Use Gist

You can use "gist" provided


by Github to share links to
your code snippets in
comments. To open gist site
click here.
Labels

Android (4)
Ant (3)
Automation (4)
Java (2)

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

?
public class FacebookScrollWebDriver {
RemoteWebDriver driver;
@Before
public void startup(){
DesiredCapabilities cap=new DesiredCapabilities();
cap.setJavascriptEnabled(true);
cap.setBrowserName("firefox");
try {
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub
} catch (MalformedURLException e) {
e.printStackTrace();
}
}

@Test
public void testScroll() throws InterruptedException{
driver.get("http://www.facebook.com");
driver.findElement(By.id("email")).click();
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("facebook id");
driver.findElement(By.id("pass")).clear();
driver.findElement(By.id("pass")).sendKeys("facebook pswd");
driver.findElement(By.xpath("//input[@type='submit']")).click();
Thread.sleep(5000);
driver.switchTo().defaultContent();
//Following is the code that scrolls through the page

Sign In

Junit (2)
Locator Strategy (1)
Maven (2)
Selenium (11)
Selenium Grid (5)
Selenium Reports (2)
Selenium-2 (7)
TestNG (10)
Subscribe To

Posts
Comments

Follow by Email

Email address...

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

for (int second = 0;; second++) {


if(second >=60){
break;
}
driver.executeScript("window.scrollBy(0,200)", "");
Thread.sleep(3000);
}

}
@After
public void stop(){
driver.quit();
}
public boolean isElementPresent(RemoteWebDriver driver, By by){
try{
driver.findElement(by);
return true;
}catch(NoSuchElementException e){
return false;
}
}
}

Submit

The following code is written for Selenium-1.0 users:


My LinkedIn Profile

About Me

View my complete profile


Followers

Join this site


w ith Google Friend Connect

Members (101) More

Already a member? Sign in

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

public class FaceScrollSeleniumOne {

Selenium selenium;
@Before
public void startup(){
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.facebook.com
selenium.start();
}
@Test
public void testScroll() throws InterruptedException{
selenium.open("/");
selenium.type("id=email", "facebook id");
selenium.type("id=pass", "facebook pswd");
selenium.click("//input[@type='submit']");
Thread.sleep(5000);
//Following is the code that scrolls through the page
for (int second = 0;; second++) {
if(second >=60){
break;
}
selenium.getEval("window.scrollBy(0,200)");
}

Thread.sleep(3000);

}
@After
public void stop(){
selenium.stop();
}
}

Posted by V arun Menon at 6:29 PM


+1 Recommend this on Google

Labels: Selenium, Selenium-2

15 comments:
Arun May 24, 2012 at 3:21 AM
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?
Reply

Replies
Varun Menon

May 24, 2012 at 4:31 PM

@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.
Reply

Aneta July 24, 2012 at 12:39 PM


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)", "");
Reply
Replies
Varun Menon

July 24, 2012 at 3:44 PM

Thanks @Aneta
Reply

Ranjana August 28, 2012 at 3:50 PM


Thaks Varun ,this post has helped me :)
Ranjana
Reply

gS September 25, 2012 at 9:20 AM


How about scrolling a table horizontaly inside a webpage ?how will we do it ?
Reply
Replies
Varun Menon

September 28, 2012 at 4:02 PM

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


Reply

keshav kumar November 16, 2012 at 5:43 PM


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
Reply
Replies
Varun Menon

November 19, 2012 at 7:58 PM

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.

sai krishna April 16, 2013 at 10:19 PM


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 .??
Reply

rajan November 20, 2012 at 3:22 PM


Really your solution helps me varun.
Thank You :)
Reply

vibhu May 23, 2013 at 11:57 PM


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

jashobanta dehury September 30, 2013 at 6:26 PM


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
Reply
Replies
Varun Menon

October 1, 2013 at 4:53 PM

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/JavascriptE
xecutor.html#executeScript(java.lang.String, java.lang.Object...)
Reply

Nisha November 28, 2013 at 11:57 AM


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.
Reply

Enter your comment...

Comment as:

Publish

Google Account

Preview

Newer Post

Home

Older Post

Subscribe to: Post Comments (Atom)

All rights reserved by http://blog.varunin.com and author of the blog until explicitly mentioned. Simple template. Powered by Blogger.