How To Scroll Web Page In Selenium Webdriver Testing Script Using Java

If you want to implement selenium scroll to top or selenium scroll to element scroll web page effect in your selenium testing script using java, you can use the org.openqa.selenium.JavascriptExecutor class which is provided by the Selenium WebDriver. You can run any JavaScript code use JavascriptExecutor object’s executeScript method. In this article, we will show you how to implement scroll web page action in selenium testing by executing javascript with java examples.

1. Selenium Scroll To Bottom Example.

  1. Below source code can scroll from webpage top to bottom using java.
  2. It will execute the javascript window.scrollTo(0, document.body.scrollHeight) in selenium WebDriver source code like below.
    ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");

2. Selenium Scroll To Top Example.

  1. After scroll to web page bottom, we will now scroll back to the top of the web page.
  2. We can run the javascript window.scrollTo(0, 0) in selenium java source code to implement it.
    ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, 0)");

3. Selenium Scroll To Element Example.

  1. We can scroll the webpage to a specified Html web element by executing javascript in Selenium WebDriver java source code also.
  2. First, we should get the WebElement object by it’s locator such as the Html web element XPath value.
    WebElement element = driver.findElement(By.xpath("//*[@class=\"next page-numbers\"]"));
  3. Then we can run the javascript arguments[0].scrollIntoView(); on the above WebElement object.
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);

4. Selenium Scroll To Special Coordinates.

  1. This is very simple, we can run the javascript window.scrollBy(x_coordinate, y_coordinate) in Selenium WebDriver java source code to implement it.
    ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");

5. Selenium Scroll Web Page Example.

  1. Create a java class com.dev2qa.webdriver.TestScrollWebPage in your java maven project.
  2. Then copy the below source code into it. The method name is very straightforward, you can see the code comments for a detailed explanation.
    package com.dev2qa.webdriver;
    
    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;
    
    public class TestScrollWebPage {
    
        static WebDriver driver;
        
        static String URL = "http://dev2qa.com/";
        
        public static void main(String[] args) {
            
            TestScrollWebPage obj = new TestScrollWebPage();
            
            obj.setUp();
            
            obj.scrollToWebPageBottom();
            
            obj.scrollToWebPageTop();
            
            obj.scrollToWebPageElemen();
            
            obj.scrollWebpageByCoordinates();
            
            obj.tearDown();
            
        }
    
    
        public static void setUp() {
            System.out.println("@BeforeClass Method setup() started.");
            /* Initiate the Firefox driver. */
            driver = new FirefoxDriver();
            
            /* Start browse page http://dev2qa.com/. */
            driver.get(URL);
            
            /* Maximize the browser window. */
            driver.manage().window().maximize();
            
            System.out.println("@BeforeClass Method setup() finished.");
        }
    
    
        public void scrollToWebPageBottom() {
            try
            {
                System.out.println("@Test method scrollToWebPageBottom started.");
                
                /* Navigate to the page http://dev2qa.com */
                driver.navigate().to(URL);
                
                /* Cast driver object to JavascriptExecutor object,
                 * then execute the java script to scroll to the bottom of the page. */
                ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
                 
                System.out.println("@Test method scrollToWebPageBottom finished.");
                
                /* Thread sleep 3 seconds to let user to see the result */
                Thread.sleep(3000);
                 
            }catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
        
        public void scrollToWebPageTop() {
            try
            {
                System.out.println("@Test method scrollToWebPageTop started.");
                
                /* Navigate to the page http://dev2qa.com */
                //driver.navigate().to(URL);
                
                /* Cast driver object to JavascriptExecutor object,
                 * then execute the java script to scroll to the bottom of the page. */
                ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, 0)");
                 
                System.out.println("@Test method scrollToWebPageTop finished.");
                
                /* Thread sleep 3 seconds to let user to see the result */
                Thread.sleep(3000);
    
            }catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
    
        public void scrollToWebPageElemen() {
            try
            {
                System.out.println("@Test method scrollToWebPageElemen started.");
            
                /* Navigate to the page http://dev2qa.com */
                //driver.navigate().to(URL);
                
                /* Get the Next page web element in the bottom of the page use web element's xpath. */
                WebElement element = driver.findElement(By.xpath("//*[@class=\"next page-numbers\"]"));
                
                /* Cast driver object to a JavascriptExecutor object, 
                 * then use that object to run javascript to scroll to the web element. */
                ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);
            
                System.out.println("@Test method scrollToWebPageElemen finished.");
                
                Thread.sleep(3000);
            }catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
    
        public void scrollWebpageByCoordinates() {
            try
            {
                System.out.println("@Test method scrollWebpageByCoordinates started.");
                
                /* Navigate to the page http://dev2qa.com */
                driver.navigate().to(URL);
                
                /* Cast driver object to a JavascriptExecutor object, then run the javascript to scroll to the position (0, 500)*/
                ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");
                
                System.out.println("@Test method scrollWebpageByCoordinates finished.");
                Thread.sleep(3000);
            }catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
        
    
        public static void tearDown() {
            System.out.println("@AfterClass Method tearDown() started.");
            driver.quit();
            System.out.println("@AfterClass Method tearDown() finished.");
        }
        
    }
    
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x