Open In App

Selenium Scrolling a Web Page using Java

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.






// Importing the Selenium libraries
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)
    {
 
        // specify the location of the driver
        System.setProperty(
            "webdriver.chrome.driver","C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
 
        // Initialising the driver
        WebDriver driver = new ChromeDriver();
 
        // launch the website
        driver.get("https://www.geeksforgeeks.org/");
 
        // Maximize the screen
        driver.manage().window().maximize();
 
        // Stating the Javascript Executor driver
        JavascriptExecutor js = (JavascriptExecutor)driver;
 
        // Scroll to bottom of page
        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.




// Importing the Selenium libraries
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) {
          
           //specify the location of the driver
           System.setProperty("webdriver.chrome.driver","C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
      
           //Initialising the driver
           WebDriver driver = new ChromeDriver();
      
           //launch the website
           driver.get("https://www.geeksforgeeks.org/");
            
           // Maximize the screen
           driver.manage().window().maximize();
            
           // Stating the Javascript Executor driver
           JavascriptExecutor js = (JavascriptExecutor) driver;
            
           // Find Problem of the day text
           WebElement element = driver.findElement(By.xpath("// *[contains(text(),'Problem of the day')]"));
            
           // Scroll to the specific position
           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.




// Importing the Selenium libraries
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) {
          
           //specify the location of the driver
           System.setProperty("webdriver.chrome.driver","C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
      
           //Initialising the driver
           WebDriver driver = new ChromeDriver();
      
           //launch the website
           driver.get("https://www.geeksforgeeks.org/");
            
           // Stating the Javascript Executor driver
           JavascriptExecutor js = (JavascriptExecutor) driver;
            
           // Scroll both horizontally and vertically
           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.




// Importing Selenium libraries
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) {
          
            //specify the location of the driver
           System.setProperty("webdriver.chrome.driver","C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
      
           //Initialising the driver
           WebDriver driver = new ChromeDriver();
      
           // Open the Geeks For Geeks website
           driver.get("https://www.geeksforgeeks.org/");
      
           // Maximize the screen
           driver.manage().window().maximize();
      
           // Add implicit wait of 10 seconds
           driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     
            // Stating the Javascript Executor driver
           JavascriptExecutor js = (JavascriptExecutor) driver;
          
           // Return the total height of the webpage
           long initialHeight = (long) js.executeScript("return   document.body.scrollHeight");
            
           // Start a while loop
           while(true){
                
               // Do infinite scrolling
               js.executeScript("window.scrollTo(0,document.body.scrollHeight)");
                
            // Add implicit wait of 10 seconds
               driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
                
               // Get current height of the page
               long currentHeight = (long) js.executeScript("return document.body.scrollHeight");
                
               // Steop when initial height equals to current height
               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.




// Importing the Selenium libraries
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) {
          
           //specify the location of the driver
           System.setProperty("webdriver.chrome.driver","C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
      
           //Initialising the driver
           WebDriver driver = new ChromeDriver();
      
           //launch the website
           driver.get("https://www.geeksforgeeks.org/");
            
           // Maximize the screen
           driver.manage().window().maximize();
            
           // Stating the Javascript Executor driver
           JavascriptExecutor js = (JavascriptExecutor) driver;
            
           // Scroll to top of page
           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.




// Importing the Selenium libraries
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) {
          
           //specify the location of the driver
           System.setProperty("webdriver.chrome.driver","C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
      
           //Initialising the driver
           WebDriver driver = new ChromeDriver();
      
           //launch the website
           driver.get("https://www.geeksforgeeks.org/");
            
           // Maximize the screen
           driver.manage().window().maximize();
            
           // Stating the Javascript Executor driver
           JavascriptExecutor js = (JavascriptExecutor) driver;
          
           // Scrolling a webpage vertically
           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.


Article Tags :