Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Scrolling webpage with Selenium Webdriver

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 855
    Comment on it

    Sometimes we face the problem of scrolling an element as per user view.

    JavaScript Executor is used in java code to scroll the page.

    There are some scenarios for which we have to perform scroll action:

    • Sometimes we require to scroll the page up and then perform actions or activity.


    • Sometimes we need to scroll the page down to the bottom and then perform action.


    • Sometimes we require to scroll the page to the element and then perform actions or activity.


    • We can also scroll the webpage to a particular position using the coordinates of the page.

    Scroll Down:
    Firstly We need to import javascriptExecutor package:

    import org.openqa.selenium.JavascriptExecutor;

    WebDriver driver = new FirefoxDriver();
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("scroll(0, 200)"); //y value '200' can be altered

    Scroll up:

    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("scroll(200, 0)"); //x value '250' can be altered

    Scroll bottom of the Page:

    JavascriptExecutor jse = (JavascriptExecutor)driver;

    Scroll to particular webelement:

    Point scrollItem =driver.findElement(By.xpath("Value")).getLocation();
    ((JavascriptExecutor)driver).executeScript("return window.title;");    
    ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", driver.findElement(By.xpath("Value')]")));

    Sample code:

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;
    public class PageScrolling {
     WebDriver driver;
     String URL = "" ;
     public void setUp() {
      driver = new FirefoxDriver();
     public void scrollingToBottom() {
       ((JavascriptExecutor) driver)
             .executeScript("window.scrollTo(0, document.body.scrollHeight)");
     public void scrollingToElement() {
      WebElement element = driver.findElement(By.linkText("Import/Export"));
      ((JavascriptExecutor) driver).executeScript(
                    "arguments[0].scrollIntoView();", element);
     public void scrollingByCoordinates() {
      ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");
     public void tearDown() {


 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: