How to perform Page scrolling with Selenium WebDriver

In my previous post i explained Selenium xpath Tutorials,Selenium CSS Selectors , Data driven Framework creation ,Please read those topics which is very important in Selenium to identify the elements and read data from excel file.In this topic i am giving explanation to scroll the page in Selenium.Let's see the script with example.


How to perform Page scrolling in Selenium WebDriver

How to perform Page scrolling in Selenium WebDriver



How to do it?

1)Create ScrollBar class under any named package with @BeforeTest and @Test TestNG annotations as below.


public class ScrollBar {

public static WebDriver driver;
public static String baseUrl;

@BeforeTest
public void setup(){

driver = new FirefoxDriver();
driver.manage().window().maximize();
baseUrl="http://www.softwaretestutorials.blogspot.in";

}

@Test
public void pageScroll(){
driver.navigate().to(baseUrl);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

//Now we need to scroll down the page in order to see all the searched links
//Here we are using javascriptExecutor for page scroll down/Up
JavascriptExecutor scpag = (JavascriptExecutor)driver;
scpag.executeScript("window.scrollBy(500,5000)", "");
}
}

 2)Creating 

JavascriptExecutor scpag = (JavascriptExecutor)driver;
JavascriptExecutor object to call ScrollBy(x,y) for coordinates to scroll the page.

Now we need to scroll down the page in order to see all the searched links,Here we are using javascriptExecutor  Interface for page scroll down/Up.

JavascriptExecutor scpag = (JavascriptExecutor)driver;
scpag.executeScript("window.scrollBy(500,5000)", "");

3)Execute the Test script by right click on Script then Run as - TestNG Test,Browser will open specified URL and it will scroll down the page until specified co ordinates in ScrollBy(x,y).

scpag.executeScript("window.scrollBy(500,5000)", "");

Please provide your valuable comments on this post,Thank you for reading my blog.