Scroll Web Page

We can scroll the web page using JavaScript Executor in the java code. I have taken the below examples with Five different scenarios of scrolling a webpage.

  1.  Scroll Down
  2.  Scroll up
  3.  Scroll bottom of the Page
  4.  Full scroll to bottom in slow motion
  5. automatically to your Web Element

1.Scroll Down:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class PageScrollDown {

	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		//Launching the browser application
		driver.get("http://www.javaseleniumworld.com/");
		//Adding wait
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		//Maximize window
		driver.manage().window().maximize();
		JavascriptExecutor jse = (JavascriptExecutor) driver;
		//Scroll Down
		jse.executeScript("scroll(0, 250)"); //y value '250' can be altered
	}
}

Note: For Scrolling Horizontally You have to alter the value inside scroll() Method.

2.Scroll up:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class PageScrollUp {

	public static void main(String[] args) throws InterruptedException {
		WebDriver driver = new FirefoxDriver();
		//Launching the browser application
		driver.get("http://www.javaseleniumworld.com/");
		//Adding wait
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		//Maximize window
		driver.manage().window().maximize();
		JavascriptExecutor jse = (JavascriptExecutor) driver;
		//Scroll Down
		jse.executeScript("scroll(0, 650)"); //y value '650' can be altered
		Thread.sleep(5000);
		//Scroll Up
		jse.executeScript("scroll(650, 0)"); //x value '650' can be altered

	}
}

3.Scroll bottom of the Page:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ScrollupToBottom {

	public static void main(String[] args) throws InterruptedException {
		WebDriver driver = new FirefoxDriver();
		//Launching the browser application
		driver.get("http://www.javaseleniumworld.com/");
		//Adding wait
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		//Maximize window
		driver.manage().window().maximize();
		JavascriptExecutor jse = (JavascriptExecutor) driver;
		jse.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));");

	}
}

You can perform the same task using Actions Class Also:

Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();

4.Full scroll to bottom in slow motion:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class PageScrollSlowMotion {

	public static void main(String[] args) throws InterruptedException {
		WebDriver driver = new FirefoxDriver();
		//Launching the browser application
		driver.get("http://www.javaseleniumworld.com/");
		//Adding wait
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		//Maximize window
		driver.manage().window().maximize();
		for (int second = 0;; second++) {
			if (second >= 20) { //You can alter the value '20' 
				break;
			} ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,400)", ""); //y value '800' can be altered
			Thread.sleep(3000);
		}
	}
}

5.Scroll automatically to your WebElement:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ScrollUpToElement {

	public static void main(String[] args) throws InterruptedException {
		WebDriver driver = new FirefoxDriver();
		//Maximize window
		driver.manage().window().maximize();
		//Launching the browser application
		driver.get("http://www.javaseleniumworld.com/");
		//Adding wait
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

		((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", driver.findElement(By.xpath("//*[@id='wpcw_contact-4']/h4")));
		//You have to provide the element’s Xpath 
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *