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);
}
Comments
Help
Hi, I tried to run this set of codes in Netbeans but I faced an error saying that Class "com.scroll.page.PageScroll" does not have a main method. May I know if I am running it in a right way or did I left out something? Thanks in advance.
Junit
Its better to run as Junit class
Rightclick on the class to run-->runas Junit
Scroll And Element inside Page
Hi, scrolling of page is fine, but how could I achieve the scrolling of an element such as DIV having a scroll?
I am trying to execute the
I am trying to execute the same script,but why I am getting null pointer exception in all the three methods
scroll inside a Div in drop Down
Hi ,
There is a field which is Drop Down . There are 200 values in the drop down . I want to click on a 100th value . Can you please let me know how to do the scrolling
Selecting drop down value
You can use Select class instead of scrolling, to select required option.
Refer : http://seleniumeasy.com/selenium-tutorials/webdriver-select-methods-to-w...
It is not working when page
It is not working when page is navigated, It is working only when any one minimum action is performed on the page after that page scroll is done.
Please help why it is not working without any one action not perform on the page scroll down is not working why?
Hi, Above code works for
Hi, Above code works for scrolling entire webpage. But how can we scroll particular div on a webpage. ?
singh
actually i have to scroll inside a particular element and i tried all these codes ...but unable to scroll, can u pls provide me the code for the same
Scroll while scrapping data
How can one scroll to the end of a page while scrapping data on that page
Add new comment