![Scroll page in Selenium Webdriver](https://i0.wp.com/learn-automation.com/wp-content/uploads/2016/10/Scroll-page-in-Selenium-Webdriver.jpg?resize=617%2C420&ssl=1)
Hello, Welcome to Selenium tutorials, in this post we will see Scroll page in Selenium Webdriver using different ways.
JavaScript plays very important role in Selenium Webdriver too, earlier also we have seen how to work with JavaScript and usage of Java Script in Selenium Webdriver.
We can highlight elements as well using JavaScript.
If you are working with Mobile Test Automation using Appium then in mobile devices we have pre-defined method to scroll page
Before starting, I want to share one more point which will clear your doubts. We have two type of scroll in Selenium.
1- Scroll into view (Kindly check detailed post which covers the complete scenario)
2- Scroll page (Scroll page and Scroll down)
Selenium handle scrolling page automatically but if want to scroll page using Selenium then yes we can do easily using JavaScript.
We have method scroll(horizontal, vertical) i.e. scroll(0,400)
Note- scroll method is not a method of Webdriver, this is a method of JavaScript.
Scroll page in Selenium Webdriver
To execute JavaScript code inside Selenium script we can take the help of JavascriptExecutor
JavascriptExecutor is an Interface which is available in package org.openqa.selenium.JavascriptExecutor;
Inside this Interface we have a method called executeScript()– so whatever script you will pass as a string It will be executed by JavascriptExecutor.
Recently I have published video for the same.
Program to Scroll page in Selenium Webdriver.
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ScrollTestCase {
public static void main(String[] args) throws Exception {
// load browser
WebDriver driver=new FirefoxDriver();
// maximize browser
driver.manage().window().maximize();
// Open Application
driver.get("http://jqueryui.com");
// Wait for 5 second
Thread.sleep(5000);
// This will scroll page 400 pixel vertical
((JavascriptExecutor)driver).executeScript("scroll(0,400)");
}
}
Please comment in below section if you have any issue while executing above code
Thanks for visiting my blog. Keep in touch.
Happy Testing. 🙂
For more updates Like our Facebook Page.
For any Selenium related query Join Facebook Group.
Great article Mukesh. Thanks for sharing the same.
Hi Vetrivelan,
Very very thanks for your comments.
Hi Mukesh,
((JavascriptExecutor)driver).executeScript(“scroll(0,400)”);.
1.what is that indicates 0,400 exactly.
2.How will come to know required Element is present at 400 only.
Hi Sreedhar,
It differs application to application. 400 is the Y coordinate of the element. You can find same using Point class. Or else you do scroll into view, for more details, please visit this link http://learn-automation.com/how-to-scroll-into-view-in-selenium-webdriver/
Hi Mukesh
Thanks for your support and wish you happy New Year
Thanks Mukesh, you are helping a big test world ..great work!!
Thanks Nitesh
Hi Mukesh,
I just had a simpler approach for scrolling, it did work for me. Please find the code below:
public WebDriver selectProject() throws InterruptedException {
Properties properties = TTXProperties.getTTxProperties();
webElement=webDriver.findElement(By.xpath(properties.getProperty(“projectName”)));
while (!isElementPresent(By
.xpath(properties.getProperty(“projectName”)))) {
webDriver.switchTo().activeElement().sendKeys(Keys.PAGE_DOWN);
}
Actions actions = new Actions(webDriver);
actions.moveToElement(
webDriver.findElement(By.xpath(properties
.getProperty(“projectName”)))).doubleClick().perform();
return webDriver;
}
public boolean isElementPresent(By locatorKey) {
try {
webDriver.findElement(locatorKey);
return true;
} catch (org.openqa.selenium.NoSuchElementException e) {
return false;
}
Hey Chetan,
Thanks for the new solution.
Scrolling up or down the page is not at all working for me. Below is my code snippet :
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript(“window.scrollBy(0,1000)”, “”); //y value ‘250’ can be altered
Thread.sleep(2000);
List tabs = driver.findElements(By.xpath(“.//*[@id=’1′]/div/div[2]/table/tbody/tr/td/ul/span”));
int noOftabs = tabs.size();
System.out.println(“noOftabs is ” + noOftabs);
for (int i = 1; i <= noOftabs; i++)
{
String tabName = tabs.get(i).getText();
System.out.println("tabname is " + tabName + i);
if (tabName.equals("Temp")) {
WebElement check = driver.findElement(By.xpath(".//*[@id='1']/div/div[2]/table/tbody/tr/td/ul/span[" + noOftabs + "]/input"));
System.out.println(" tabName " + tabName + "i is " + i);
Thread.sleep(4000);
check.click();
System.out.println(" Selected " + check.isDisplayed());
}
}
System.out.println("checkbox selected");
Thread.sleep(4000);
// JavascriptExecutor js = (JavascriptExecutor) driver;
jse.executeScript("scroll(0, -250);"); //x value '250' can be altered
Thread.sleep(2000);
I use scroll method to scroll but you have used scrollBy
Thanks vertical scroll worked. But the above mentioned site did not had horizontal scroll. Can you share a site where horizontal scoll can be tested
Good question let me check.
Hi mukesh,
I would like to know whether this is possible in selenium or not. Suppose i have a page which will load by scrolling (shopping portal ). How do you I come to know that list of items loaded.
Yes Rakesh it is possible. ScrollIntoView will work in this case.
Hi Mukesh ,
I tried same method for scroll horizontal by passing below method
js.executeScript(“scroll(250, 0)”);
But it is not working for me
can u please suggest how to proceed scroll right
Hi Aswini,
Can u try below code
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(“window.scrollBy(2000,0)”, “”);
Hi Mukhesh,
This dint work for me for horizontal scrolling 🙁 Instead I had to use the below code (somewhere i found in stack overflow) using “Actions”
Actions dragger = new Actions(driver);
WebElement draggablePartOfScrollbar = driver.findElement(By.xpath(“/html/body/section[2]/div/div[2]/div/div/div”));
int numberOfPixelsToDragTheScrollbarRight = 5000;
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(numberOfPixelsToDragTheScrollbarRight ,0).release().perform();
Thanks Ramya for update.
Hi,
I am unable to scroll down on a page on a mobile app on an android device using the above method. I got an error as “Method has not been implemented yet”. I tried using Actions but still I am getting the same error.
I tried using swipe method but I am unable to scroll down. Can you please provide me a solution .
MobileElement autoReorder=
driver.findElement(By.xpath(“//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[3]/android.view.View[2]/android.view.View[1]”));
autoReorder.click();
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript(“scrollBy(0,2500)”);
//TouchAction action = new TouchAction((MobileDriver)driver);
//Dimension size = driver.manage().window().getSize();
//System.out.println(size);
//Find swipe start and end point from screen’s with and height.
//Find starty point which is at bottom side of screen.
//int starty = (int) (size.height * 0.60);
//Find endy point which is at top side of screen.
//int endy = (int) (size.height * 0.30);
//Find horizontal point where you wants to swipe. It is in middle of screen width.
//int startx = size.width / 2;
//System.out.println(“starty = ” + starty + ” ,endy = ” + endy + ” , startx = ” + startx);
//Swipe from Bottom to Top.
//driver.swipe(startx, starty, startx, endy, 3000);
// Thread.sleep(2000);
//Swipe from Top to Bottom.
//driver.swipe(startx, endy, startx, starty, 3000);
//Thread.sleep(2000);
For mobile use below link
http://learn-automation.com/scroll-in-appium-for-android-and-ios/
But scrollTo method is deprecated now. I am unable to use the swipe method for scrolling. Are there any other methods to do scrolling apart from “swipe”? I have one more question, Can we not use Actions and Javascript executor methods in appium ?
Hi Nivi,
Agree its deperecated in latest releases but if you use 3.2 then it will work. You can use Action and Java Script command if using Chrome Mobile Browser
I really appreciate this Selenium Stuff its so heal full…
Thanks Mate 🙂
Hi Mukesh,
Can you please tell me how to scroll an element up and down? I have a list which is having scroll bar and I need to scroll by dragging the scroll bar in that list
Hi Varsha,
Here you go Please check post and video too and let me know if you are finding any issue.
http://learn-automation.com/how-to-scroll-into-view-in-selenium-webdriver/
This can also be executed by using js.executeScript(“window.scrollBy(0,4500)”);
Thank you so much for your detailed description.description. Iti s really helpfull.
Thanks Saroj 🙂 Keep visiting.
Simple and very easy one.
Hi Mukesh,
Whether the above code will work for both scroll up and scroll down??
Hi aaa,
Yes this will work for both scroll up and scroll down but you just need to specify the correct ‘y’ coordinate. For e.g.
js.executeScript(“window.scrollBy(0,450)”); //scrolling down
Thread.sleep(2000);
js.executeScript(“scroll(0, -250)”); //scrolling up
Hope it helps!
Yes It is helpfull with add this as well in Post.
Thnks Nitin