selenium tutorial testing tools

Scrolling web page with Selenium Webdriver using java

We can scroll the web page using javaScript Executor in the java code.We have taken the below example with three different scenarios of scrolling a webpage.

1. We may require to scroll to bottom of the page and then perform operations. For this scenario we have created a test 'scrollingToBottomofAPage'/

2. Some times, we may require to scroll to particular element and peform operations on that particular element. For this we need to pass the element on which we need to perform operation. For this scenario we have created a test 'scrollingToElementofAPage'/

3. We can also use the coordinates to scroll to particular position by passing the coordinates. For this scenario we have created a test 'scrollingByCoordinatesofAPage'/

package com.scroll.page;

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 PageScroll {
	WebDriver driver;
	String URL = "https://www.linkedin.com/";

	@BeforeClass
	public void setUp() {
		driver = new FirefoxDriver();
		driver.get(URL);
		driver.manage().window().maximize();
	}

	@Test(priority=1)
	public void scrollingToBottomofAPage() {
		driver.navigate().to(URL);
		 ((JavascriptExecutor) driver)
         .executeScript("window.scrollTo(0, document.body.scrollHeight)");
	}

	@Test(priority=2)
	public void scrollingToElementofAPage() {
		driver.navigate().to(URL+"directory/companies?trk=hb_ft_companies_dir");
		WebElement element = driver.findElement(By.linkText("Import/Export"));
		((JavascriptExecutor) driver).executeScript(
                "arguments[0].scrollIntoView();", element);
	}
	
	@Test(priority=3)
	public void scrollingByCoordinatesofAPage() {
		driver.navigate().to(URL+"job/?trk=hb_ft_jobs");
		((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");
	}
	
	@AfterClass
	public void tearDown() {
		driver.quit();
	}
}

The more better way to do this is having a utils class and define reusable methods. We can call these methods from different classes/tests.

We need to pass the driver to the method.

 public static void scrollToBottom(WebDriver driver) {
        ((JavascriptExecutor) driver)
                .executeScript("window.scrollTo(0, document.body.scrollHeight)");
    }

In the same way, we may need to scroll to particular element and perform operations. To do this we need the below example reusable method. We need to pass the driver and element.

 public static void scrollTo(WebDriver driver, WebElement element) {
        ((JavascriptExecutor) driver).executeScript(
                "arguments[0].scrollIntoView();", element);
    }
Selenium Tutorials: 

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Wanted Contributers

Hello Viewer! We're a relatively a small group of writers who have combined efforts to write the articles. Well, we need more help from talented people to contribute and grow the size of articles. It's great work if you're looking for a little money on the side – make this as your Part time job.

We are mainly looking into Protractor, Cucumber, JBehave, RSpec, Behat Mink, Selenium Python or any other tool which uses selenium.

If you can contribute on any the above, Please contact us.