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();

  }
     
}

0 comments:

Post a Comment