top of page

Scrolling A Web Page

​

​

What is ScrollBar in Web Applications ?

​

A scrollbar moves the window up-down and left right direction if the current page scroll does not fit the visible area of the screen.

It is of two types :

  • Vertical scroll

  • Horizontal scroll

​

Scroll in Selenium

To automate scrolling selenium webdriver provides an interface JavascriptExecutor .

import org.openqa.selenium.JavascriptExecutor;

​

Create object for JavascripExecutor :

JavascriptExecutor  jse=(JavascriptExecutor) driver;

 

Perform scrolling by scrollBy() method :

 jse.executeScript("window.scrollBy(x-axis pixel,y-axis pixel)");

​

​

Vertical scroll

  • Vertically scroll from top to bottom or bottom to top :

Downside : jse.executeScript("window.scrollBy(0,document.body.scrollHeight)");

Upside : jse.executeScript("window.scrollBy(0,-document.body.scrollHeight)");

​

  • Vertically scroll by specified pixel value :

Downside : jse.executeScript("window.scrollBy(0,3500)");

Upside : jse.executeScript("window.scrollBy(0,-3500)");

​

  • Vertically scroll by the visibility of webelement :

 WebElement Element=driver.findElement(By.cssSelector("a[href='/open-source/maven-plugins']"));

 jse.executeScript("arguments[0].scrollIntoView();",Element);

​

​

Vertical Scrolling from top to bottom and bottom to top =>

​

package seleniumexamples;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ScrollVertically_Top_Bottom {

    public static void main(String[] args) throws InterruptedException {
       // TODO Auto-generated method stub
       WebDriver driver;
       String
baseURL="https://mvnrepository.com/";
       
     
 //Launch the web browser
       System.setProperty("webdriver.chrome.driver","..\\SeleniumJava\\drivers\\chromedriver99.exe");
       
driver=new ChromeDriver();
       

       //To maximize the size of window
       driver.manage().window().maximize();
       

       //Navigate through an URL
       driver.get(baseURL);
       

       //Perform scroll on a particular web page....
       JavascriptExecutor  jse=(JavascriptExecutor) driver;
       //Scroll to bottom...
       jse.executeScript("window.scrollBy(0,document.body.scrollHeight)");
       Thread.sleep(3000);

       //Scroll to top...
       jse.executeScript("window.scrollBy(0,-document.body.scrollHeight)");
       Thread.sleep(3000);

        //close all the windows 
       driver.quit();
   }

}

 

 

Vertically scroll down and up by specified pixel and by the visibility of WebElement =>

​

package seleniumexamples;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Scroll_VerticallyAsPerPixelAndToElement {

    public static void main(String[] args) throws InterruptedException {
     
 // TODO Auto-generated method stub
       WebDriver driver;
       String
baseURL="https://mvnrepository.com/";
       
       
//Launch the web browser
       System.setProperty("webdriver.chrome.driver","..\\SeleniumJava\\drivers\\chromedriver99.exe");
       
driver=new ChromeDriver();
       

       //To maximize the size of window
       driver.manage().window().maximize();
       

       //Navigate through an URL
       driver.get(baseURL);
       WebElement
Element=driver.findElement(By.cssSelector("a[href='/open-source/maven-plugins']"));
       

       //Perform scroll on a particular web page....
       JavascriptExecutor  jse=(JavascriptExecutor) driver;
       //Scroll down by a specified pixel...
       jse.executeScript("window.scrollBy(0,3500)");
       Thread.sleep(3000);

       //Scroll up by a specified pixel...
       jse.executeScript("window.scrollBy(0,-3500)");
       Thread.sleep(3000);

       //Scroll vertically till the element is found...
       jse.executeScript("arguments[0].scrollIntoView();",Element);
       Thread.sleep(3000);

        //close all the windows 
       driver.quit();

    }

}

​

​

Horizontal scroll

  • Horizontally scroll from right to left or left to right :

Right Side : jse.executeScript("window.scrollBy(document.body.scrollHeight,0)");

Left Side : jse.executeScript("window.scrollBy(-document.body.scrollHeight,0)");

​

  • Horizontally scroll by specified pixel value :

Right Side : jse.executeScript("window.scrollBy(4000,0)");

Left Side : jse.executeScript("window.scrollBy(-5500,0)");

​

  • Horizontally scroll by the visibility of webelement :

WebElement Element=driver.findElement(By.xpath("//img[@src='images/silly_questions.gif']"));

jse.executeScript("arguments[0].scrollIntoView();",Element);

​

Horizontal Scrolling from left to right and right to left =>

​

package seleniumexamples;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ScrollHorizontally_Left_Right {

    public static void main(String[] args) throws InterruptedException {
     
 // TODO Auto-generated method stub
       WebDriver driver;
       String
baseURL="https://dashboards.handmadeinteractive.com/jasonlove/";
       

       //Launch the web browser
       System.setProperty("webdriver.chrome.driver","..\\SeleniumJava\\drivers\\chromedriver99.exe");
       
driver=new ChromeDriver();
       

       //To maximize the size of window
       driver.manage().window().maximize();
       //Navigate through an URL
       driver.get(baseURL);
       

       //Perform scroll on a particular web page....
       JavascriptExecutor  jse=(JavascriptExecutor) driver;
       //Scroll to right...
       jse.executeScript("window.scrollBy(document.body.scrollHeight,0)");
       Thread.sleep(3000);

       //Scroll to left...
       jse.executeScript("window.scrollBy(-document.body.scrollHeight,0)");
       Thread.sleep(3000);
       
       
driver.close();
   }

}

​

​

Horizontally scroll right and left by specified pixel and by the visibility of WebElement =>

​

package seleniumexamples;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Scroll_HorizontallyAsPerPixelAndToElement {

    public static void main(String[] args) throws InterruptedException {
       // TODO Auto-generated method stub
       WebDriver driver;
       String
baseURL="https://dashboards.handmadeinteractive.com/jasonlove/";
       

       //Launch the web browser
       System.setProperty("webdriver.chrome.driver","..\\SeleniumJava\\drivers\\chromedriver99.exe");
       
driver=new ChromeDriver();
       
       
//To maximize the size of window
       driver.manage().window().maximize();
       

       //Navigate through an URL
       driver.get(baseURL);
       WebElement
Element=driver.findElement(By.xpath("//img[@src='images/silly_questions.gif']"));
       

       //Perform scroll on a particular web page....
       JavascriptExecutor  jse=(JavascriptExecutor) driver;
       //Scroll right by a specified pixel...
       jse.executeScript("window.scrollBy(4000,0)");
       Thread.sleep(3000);

       //Scroll left by a specified pixel...
       jse.executeScript("window.scrollBy(-5500,0)");
       Thread.
sleep(3000);
       //Scroll horizontally till the element is found...
       jse.executeScript("arguments[0].scrollIntoView();",Element);
       Thread.sleep(3000);

        //close all the windows 
       driver.quit();

    }

}
 

​

​

​

​

​

Refer next page Selenium Assertions

bottom of page