An open-source framework that is used for automating web applications is known as Selenium. Selenium handles various operations, like opening of website, clicking buttons, scrolling the webpage, etc. In this article, we have defined the numerous ways to scroll a webpage using Selenium in Java.
Selenium Scrolling a Web Page using Java
Now, we will discuss the various ways to scroll the webpage using Selenium webdriver in Java.
Scroll Down to the Page’s Bottom
If the user knows the element he is finding for further actions is at the bottom of the page, then this is the best approach. In this approach, we have opened the Geeks For Geeks website (link) and then scrolled to the bottom of the page.
Java
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class selenium3 {
public static void main(String[] args)
{
System.setProperty(
"webdriver.chrome.driver" , "C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript( "window.scrollTo(0, document.body.scrollHeight)" );
}
}
|
Output:

Scroll Based on the Visibility of the Web Element on the Page
Whenever the user wants to scroll the page till the driver finds the position of the element on the webpage, it is the best approach. In this approach, we opened the Geeks For Geeks website (link) and then scrolled till the webdriver found the element containing the text ‘Problem of the day’ using the contains and findElement function.
Java
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
public class selenium3 {
public static void main(String[] args) {
System.setProperty( "webdriver.chrome.driver" , "C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.xpath( "// *[contains(text(),'Problem of the day')]" ));
js.executeScript( "arguments[0].scrollIntoView();" , element);
}
}
|
Output:

Scroll a Webpage Both Horizontally and Vertically
This is the best approach if the user knows he needs to scroll the webpage horizontally as well as vertically to find the web element. In this approach, we have opened the Geeks For Geeks website (link) and then scrolled the webpage both horizontally and vertically.
- Horizontal Scroll: Here we have scrolled the webpage horizontally by 50 pixels.
- Vertical Scroll: Here we have scrolled the webpage horizontally by 1500 pixels.
Java
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class selenium3 {
public static void main(String[] args) {
System.setProperty( "webdriver.chrome.driver" , "C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript( "window.scrollBy(50,1500)" );
}
}
|
Output:

Scroll a Webpage with Infinite Scrolling
When the webpage is too large and the user wants to scroll a webpage infinitely till he reaches the end of the webpage or at the specific position he wants, then he can use this approach. In this approach, we opened the Geeks For Geeks website (link) and then scrolled infinitely till he reached the end of the webpage using a while loop and break feature.
Java
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class selenium8 {
public static void main(String[] args) {
System.setProperty( "webdriver.chrome.driver" , "C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait( 10 , TimeUnit.SECONDS);
JavascriptExecutor js = (JavascriptExecutor) driver;
long initialHeight = ( long ) js.executeScript( "return document.body.scrollHeight" );
while ( true ){
js.executeScript( "window.scrollTo(0,document.body.scrollHeight)" );
driver.manage().timeouts().implicitlyWait( 5 , TimeUnit.SECONDS);
long currentHeight = ( long ) js.executeScript( "return document.body.scrollHeight" );
if (initialHeight == currentHeight) {
break ;
}
initialHeight = currentHeight;
}
}
}
|
Output:

Scroll to the Top of the Page
Sometimes the webpage is too large and the user has scrolled to a specific position, and then if the user has to scroll back to the top of the page, this is the best approach. In this approach, we have opened the Geeks For Geeks website (link) and then scrolled to the top of the page.
Java
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class selenium3 {
public static void main(String[] args) {
System.setProperty( "webdriver.chrome.driver" , "C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript( "window.scrollTo(document.body.scrollHeight,0)" );
}
}
|
Output:

Scroll Down a Page by Specified Pixels
This is the best approach if the user knows the exact dimensions of the webpage, where the web element is present. In this approach, we have opened the Geeks For Geeks website (link) and then scrolled the webpage both horizontally and vertically.
Java
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class selenium3 {
public static void main(String[] args) {
System.setProperty( "webdriver.chrome.driver" , "C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript( "window.scrollBy(40,1000)" );
}
}
|
Output:

Conclusion
In conclusion, scrolling a webpage using Selenium WebDriver in Java can happen in various ways, from specific pixels, to the bottom of the page, to the top of the page, to infinite scrolling, etc. I hope after reading the above article, you will be able to do all types of scrolling using Selenium in Java.
FAQs
1. How to wait for content to load after scrolling?
There are 3 ways to wait for content to load after scrolling:
- Implicit wait: The function that sets the amount of time the WebDriver instance should wait in case an element webdriver is found if it’s not immediately available is known as implicit wait.
- Explicit wait: The way of telling Selenium webdriver to wait for a certain amount of time before throwing any exception is known as explicit wait.
- Using sleep function: The function that stops the execution of the script for a specified amount of time is known as the sleep function.
2. Which is the best approach for scrolling a webpage?
Ans: It depends if you know the position of the element the webdriver is finding or not. In case, you know the exact position, then Scroll down a page by specified pixels in Selenium is the best approach, else Scroll based on the visibility of the Web element on the page in Selenium is the best approach.
|