Scroll Down-Up Web Page Using Javascript Executor In Selenium Webdriver

Yes, We can scroll down page using javascript executor In selenium webdriver. If you remember, Earlier I have posted many different posts where we can use javascript executor to perform different actions on web page like Highlighting Element, Generating Alert, Get Domain Name, Get Page Title etc.. Now If you needs to scroll down the page then you can use Javascript Executor In Selenium Webdriver test.

Why need to scroll down page ?
If you see some websites specially e-commerce website, More products will be displayed only If scroll down the page. Same thing on facebook website. More posts will be loaded only If you scroll down the page. In this kind of scenario, You can use javascript executor to scroll down page If you wants to take some action on page element which appears on scroll down. You can check presence of scroll as described In THIS POST.

Scroll by given pixel offset
I have created test script to scroll down-up web page In horizontal or vertical direction baser on given x y pixel offset.

Bellow given example will first scroll down(X direction) web page by 600 pixels and then It will scroll up It by 300 pixels.

package Testing_Pack;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Scrolling {
 
 WebDriver driver;
 @BeforeTest
 public void setup() throws Exception {
  driver =new FirefoxDriver();     
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.com");
 }
 
 @Test
 public void Scroll_Page() throws IOException, InterruptedException {
  //To scroll down web page by 600 pixels In x(vertical) direction.  
  //You can y parameter to scroll page In horizontal direction.
  JavascriptExecutor javascript = (JavascriptExecutor) driver;
  javascript.executeScript("window.scrollBy(0,600)", "");
  
  Thread.sleep(3000);
  
  //To scroll up web page by 300 pixels In x(vertical) direction.
  javascript.executeScript("window.scrollBy(0,-300)", "");
  }
}

Scroll down to bottom of page
If you wants to scroll down to bottom of the page, You can use bellow given @Test method In above example.

@Test
 public void Scroll_Page() throws IOException, InterruptedException {
  //Scroll down to bottom of the page.
  JavascriptExecutor javascript = (JavascriptExecutor) driver;
  javascript.executeScript("window.scrollTo(0, document.body.scrollHeight)", "");  
 }

Scroll to element In selenium WebDriver
If you wants to scroll till some element on page then you can use element parameter In javascript as shown In bellow example.
//Scroll till element.
JavascriptExecutor je = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.xpath("//div[@id='dragdiv']"));
je.executeScript("arguments[0].scrollIntoView(true);",element);

4 comments:

  1. thanks very informative

    ReplyDelete
  2. How do we scroll down/up vertically in webtable records which is present inside Webpage. Note: Vertical scroll bar not present for whole web page. Only for Webtable.

    ReplyDelete
  3. thanks a lot for telling all different types of scroll command in one post. I would like to add one more

    js.executeScript("scroll(0,1500);");

    This can also do a scroll

    ReplyDelete
  4. Is there any solution to scroll up and down without using JavascriptExecutor?

    ReplyDelete