Tuesday 16 January 2018

How to Scroll using Selenium WebDriver

In this post we are going to talk about how exactly we can scroll this using Selenium. Unfortunately Selenium does not have inbuilt method which allow us to scroll into view but, we can scroll into view in Selenium using JavaScript executor.


Now let us see the detailed code on how to Scroll using Selenium WebDriver



package test;


import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


public class ScrollTest {
 
     @Test
 public  void test() {

  // Start browser
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();


  // Maximize browser

  driver.manage().window().maximize();



  // Pass application URL

  driver.get("http://www.qaautomated.com");



  // Create instance of Javascript executor

  JavascriptExecutor je = (JavascriptExecutor) driver;



  //Identify the WebElement which will appear after scrolling down

  WebElement element = driver.findElement(By.xpath(".//*[@id='colophonpbt']"));



  // now execute query which actually will scroll until that element is not appeared on page.

  je.executeScript("arguments[0].scrollIntoView(true);",element);



  // Extract the text and verify

  System.out.println(element.getText());
  
  //close browser
  driver.quit();

  }
     
}

2 comments:

  1. Done a great job by posting the information onn this technology.
    Its really easy to understand and thank you for posting.
    selenium training course
    selenium training classes
    selenium classes
    selenium training course

    ReplyDelete
  2. The above article regarding scroll on selenium seems so informative. This helps people like us to understand the concept of selenium for our Selenium training in chennai Please do share more informations and thanks a lot.

    ReplyDelete