Home >>Selenium Tutorial >Selenium WebDriver - Scrolling a web page

Selenium WebDriver - Scrolling a web page

Selenium WebDriver - Scrolling a web page

You'll learn how to scroll down or up in this section to view additional details present on the web page.

Let us find a test case, in which the following conditions should be automated:

  • Invoke Firefox browser
  • Open URL: www.phptpoint.com
  • Scroll down the web page to reveal the section on Java Technology

Step by step, we will build our test case to offer you a complete understanding of how to scroll a web page using JavaScript's "scrollBy" method.

Step 1. Activate Eclipse IDE and open the existing test suite "Demo_Test" which we have built in earlier sessions of this tutorial.

Step 2. Right click on the "src" folder and create a new Class File from New > Class. Give your Class name as "Scroll_Test" and click on "Finish" button.

Step 3. Let's get to the coding ground.

To trigger the Firefox browser, Gecko driver must be downloaded and the device properties configured for the Gecko driver. We have covered this in previous tutorial sessions already. To know how to download and configure system properties for Firefox driver, refer to "Running check on Firefox Browser."

Here is the sample code to set system property for Gecko driver:

// System Property for Gecko Driver   
stem.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );  

We have to initialize Gecko Driver after that using the Desired Capabilities Class.

Here is the sample code for initializing gecko driver utilizing class DesiredCapabilities.

// Initialize Gecko Driver using Desired Capabilities Class  
DesiredCapabilities capabilities = DesiredCapabilities.firefox();  
capabilities.setCapability("marionette",true);  
ebDriver driver= new FirefoxDriver(capabilities);  

Combining the above two code blocks, we'll get the code snippet for the Firefox application to start.

// System Property for Gecko Driver   
System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );  
      
 // Initialize Gecko Driver using Desired Capabilities Class  
DesiredCapabilities capabilities = DesiredCapabilities.firefox();  
capabilities.setCapability("marionette",true);  
WebDriver driver= new FirefoxDriver(capabilities);  

After that we have to write the code that will simplify our second test scenario (navigate to the URL of your selection)

Here is the sample code for navigating to the URL you want to use:

// Launch Website  
navigate().to("www.phptpoint.com");   

The complete version must look like this till now:
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.firefox.FirefoxDriver;  
import org.openqa.selenium.remote.DesiredCapabilities;  
  
public class Scroll_Test {  
  
    public static void main(String[] args) {  
          
    // System Property for Gecko Driver   
        System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );  
              
    // Initialize Gecko Driver using Desired Capabilities Class  
DesiredCapabilities capabilities = DesiredCapabilities.firefox();  
            capabilities.setCapability("marionette",true);  
            WebDriver driver= new FirefoxDriver(capabilities);  
          
  
      // Launch Website  
driver.navigate().to("www.phptpoint.com");   
      
    }  
  
}

Step 4. To order to simplify our third test case, we need to compose the application that will scroll down the Java Technology section of the phptpoint website.

JavascriptExecutor js = (JavascriptExecutor)driver;  
js.executeScript("scrollBy(0, 3000)");  

Thus, our final test script will look something like this:

import org.openqa.selenium.JavascriptExecutor;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.firefox.FirefoxDriver;  
import org.openqa.selenium.remote.DesiredCapabilities;  
  
public class Scroll_Test {  
  
    public static void main(String[] args) {  
          
        // System Property for Gecko Driver   
        System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );  
              
        // Initialize Gecko Driver using Desired Capabilities Class  
            DesiredCapabilities capabilities = DesiredCapabilities.firefox();  
            capabilities.setCapability("marionette",true);  
            WebDriver driver= new FirefoxDriver(capabilities);  
          
  
        // Launch Website  
        driver.navigate().to("www.phptpoint.com");   
  
        //Scroll down the webpage by 4500 pixels  
        JavascriptExecutor js = (JavascriptExecutor)driver;  
        js.executeScript("scrollBy(0, 3000)");  
  
    }  
}