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 Demo;

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 TestScroll {

public static void main(String[] args) {

// Start browser

WebDriver driver=new FirefoxDriver();



// 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='id_sample']"));



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

}

}

0 comments:

Post a Comment