Tutorials Hut

  • Selenium WebDriver

       Selenium Introduction
       Benefits of Selenium
       Four components of Selenium
       Difference b/w Selenium IDE, RC & WebDriver
       Selenium WebDriver Architecture
       Background when user execute selenium code
       Download and Install Java
       Download and Install Eclipse
       Download Selenium WebDriver
       Selenium WebDriver Locators
       Selenium - Launch Browser
       Selenium WebDriver Waits
       Selenium- Implicit wait
       Selenium- Explicit wait
       Selenium- Fluent wait
       Selenium- Commonly used commands
       Selenium- findElement & findElements
       Selenium- Selenium-Handling check Box
       Selenium- Handling Radio button
       Selenium- Handling drop down
       Selenium- Take Screenshot
       Selenium- Handle Web Alerts
       Selenium- Multiple Windows Handling
       Selenium- Handle iframes
       Selenium- Upload a file
       Selenium- Download a file
       Selenium- Actions Class Utilities
       Selenium- Mouse Actions
       Selenium- Keyboards Events
       Selenium- Handle mouse hover Actions
       Selenium- Drag and Drop
       Selenium- Scroll a WebPage
       Selenium- Context Click / Right Click
       Selenium- Double Click
       Selenium- Desired Capabilities
       Selenium- Assertions
       Selenium- Exceptions and Exception Handling
       Selenium- Difference b/w driver.close() & driver.quit()
       Selenium- difference b/w driver.get() & driver.navigate()
       Selenium- JavascriptExecutor
       Selenium- Read excel file using Fillo API
       Selenium- Database Testing using Selenium
       Selenium- Read & write excel file using Apache POI
       Selenium- Read and Write csv file in Selenium
       Selenium- Dynamic Web Table Handling
       Selenium- Maven Integration with Selenium
       Selenium- Set up Logging using Log4j
       Selenium-Implement Extent Report



  • How to Scroll a web page using Selenium WebDriver?

    This article will present you with a complete idea about how to scroll a web page up or down using selenium web driver .

    Scroll a web Page using Selenium

    JavaScriptExecutor in Selenium is used to execute the code written in Javascript. This interface has two methods namely ExecuteScript and ExecuteAsyncScript which are used to execute the JavaScript code.

    Let’s discuss below six scenarios of scrolling a webpage

      1. How to scroll a web page vertically to perform any action ?
      2. How to  scroll down till a particular element is found?
      3. How to scroll a webpage at a particular coordinate (X and Y)?
      4. How to scroll a web page up using Selenium WebDriver?
      5. How to scroll a web page horizontally in the right direction 
      6. How to scroll a webpage  horizontally in the left direction

    There are multiple methods are in JAVA to scroll a page vertically or horizontally using Selenium WebDriver , below are the few methods

    Syntax:

    1. Scroll the page vertically to perform any action
    JavascriptExecutor js = (JavascriptExecutor) driver;  
    js.executeScript(“window.scrollTo(0, document.body.scrollHeight)”);
    package com.test;
    
    import java.io.File;
    
    import java.io.IOException;
    
    import java.util.concurrent.TimeUnit;
    
    import javax.imageio.ImageIO;
    
    import org.apache.commons.io.FileUtils;
    
    import org.openqa.selenium.By;
    
    import org.openqa.selenium.JavascriptExecutor;
    
    import org.openqa.selenium.OutputType;
    
    import org.openqa.selenium.WebDriver;
    
    import org.openqa.selenium.WebElement;
    
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class ScrollDowntillEndOfPage {
    
    public static WebDriver driver;
    
    public static void main(String[] args) throws IOException, InterruptedException {
    
    // Create a new instance of the Chrome driver
    
    System.setProperty("webdriver.chrome.driver", "D:\\Drivers\\chromedriver.exe");
    
    driver = new ChromeDriver();
    
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    // Launch the URL
    
    driver.get("https://tutorialshut.com/demo-website-for-selenium-automation-practice/");
    
    driver.manage().window().maximize();
    
     // Scroll the page vertically till end to perform any action
    
    JavascriptExecutor js = (JavascriptExecutor) driver;
    
    js.executeScript("window.scrollTo(0,document.body.scrollHeight)");
    
    Thread.sleep(5000);
    
    // close the driver
    
    driver.close();
    
    }
    
    }
    
    1.     Scroll down till a particular element found
    WebElement element = driver.findElement(By.xpath(“xpath”));
    JavascriptExecutor js = (JavascriptExecutor) driver;  
    js.executeScript(“arguments[0].scrollIntoView();”, element);

    Where ‘element’ is the locator where you want to scroll.

    Code Implementation

     
    package com.test;
    
    import java.io.File;
    
    import java.io.IOException;
    
    import java.util.concurrent.TimeUnit;
    
    import javax.imageio.ImageIO;
    
    import org.apache.commons.io.FileUtils;
    
    import org.openqa.selenium.By;
    
    import org.openqa.selenium.JavascriptExecutor;
    
    import org.openqa.selenium.OutputType;
    
    import org.openqa.selenium.WebDriver;
    
    import org.openqa.selenium.WebElement;
    
    import org.openqa.selenium.chrome.ChromeDriver;
    
    
    
    
    public class ScrollDowntillElementFound {
    
    public static WebDriver driver;
    
    public static void main(String[] args) throws IOException, InterruptedException {
    
    // Create a new instance of the Chrome driver
    
    System.setProperty("webdriver.chrome.driver", "D:\\Drivers\\chromedriver.exe");
    
    driver = new ChromeDriver();
    
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    // Launch the URL
    
    driver.get("https://tutorialshut.com/demo-website-for-selenium-automation-practice/");
    
    driver.manage().window().maximize();
    
    // identify Drop To element on page
    
    WebElement element = driver.findElement(By.xpath("//button[text()='Drop TO']"));
    
    // Scroll down till 'Drop TO' Button found
    
    JavascriptExecutor js = (JavascriptExecutor) driver;
    
    js.executeScript("arguments[0].scrollIntoView();", element);
    
    Thread.sleep(5000);
    
    // close the driver
    
    driver.close();
    
    }
    
    }
    
    1.   Scroll at a particular coordinate

    Scroll the page to position “X” horizontally and “Y” vertically:  

    JavascriptExecutor js = (JavascriptExecutor) driver;  
    js.executeScript(“window.scrollBy(X,Y)”);

    Code Implementation

    public static void main(String[] args) throws IOException, InterruptedException {
    
    // Create a new instance of the Chrome driver
    
    System.setProperty("webdriver.chrome.driver", "D:\\Drivers\\chromedriver.exe");
    
    driver = new ChromeDriver();
    
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    // Launch the URL
    
    driver.get("****URL****");
    
    driver.manage().window().maximize();
    
    //scroll the page to position 500 horizontally and 200 px vertically:  
    
    JavascriptExecutor js = (JavascriptExecutor) driver;  
    
    js.executeScript("window.scrollBy(500,200)");
    
     Thread.sleep(5000);
    
    // close the driver
    
    driver.close();
    
    }
    

    4) To Scroll web page up using Selenium WebDriver:

    JavascriptExecutor js = (JavascriptExecutor) driver;  
    js.executeScript(“window.scrollTo(document.body.scrollHeight,0)”);

    5) Scroll page horizontally in the right direction 


    JavascriptExecutor js = (JavascriptExecutor) driver;  
    js.executeScript(“window.scrollBy(500,0)”);

    //horizontal position “500”

    6) Scroll horizontally in the left direction

    JavascriptExecutor js = (JavascriptExecutor) driver;  
    js.executeScript(“window.scrollBy(-2000,0)”);



  • Selenium WebDriver Tutorials

       Selenium Introduction
       Benefits of Selenium
       Four components of Selenium
       Difference b/w Selenium IDE, RC & WebDriver
       Selenium WebDriver Architecture
       Background when user execute selenium code
       Download and Install Java
       Download and Install Eclipse
       Download Selenium WebDriver
       Selenium WebDriver Locators
       Selenium - Launch Browser
       Selenium WebDriver Waits
       Selenium- Implicit wait
       Selenium- Explicit wait
       Selenium- Fluent wait
       Selenium- Commonly used commands
       Selenium- findElement & findElements
       Selenium- Selenium-Handling check Box
       Selenium- Handling Radio button
       Selenium- Handling drop down
       Selenium- Take Screenshot
       Selenium- Handle Web Alerts
       Selenium- Multiple Windows Handling
       Selenium- Handle iframes
       Selenium- Upload a file
       Selenium- Download a file
       Selenium- Actions Class Utilities
       Selenium- Mouse Actions
       Selenium- Keyboards Events
       Selenium- Handle mouse hover Actions
       Selenium- Drag and Drop
       Selenium- Scroll a WebPage
       Selenium- Context Click / Right Click
       Selenium- Double Click
       Selenium- Desired Capabilities
       Selenium- Assertions
       Selenium- Exceptions and Exception Handling
       Selenium- Difference b/w driver.close() & driver.quit()
       Selenium- difference b/w driver.get() & driver.navigate()
       Selenium- JavascriptExecutor
       Selenium- Read excel file using Fillo API
       Selenium- Database Testing using Selenium
       Selenium- Read & write excel file using Apache POI
       Selenium- Read and Write csv file in Selenium
       Selenium- Dynamic Web Table Handling
       Selenium- Maven Integration with Selenium
       Selenium- Set up Logging using Log4j
       Selenium-Implement Extent Report


































  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    Scroll to Top