Hello friends,
In this article we will discuss that how to perform scroll operation in selenium.
In this article we will discuss that how to perform scroll operation in selenium.
For Video :- Click Here
Watch Video for more Understanding
While automating an application we generally found that we need to perform here scroll Up or scroll down or Scroll up to an element, So in this demo we will learn that how to perform these all operations.
In selenium, we have JavascriptExecutor interface, to perform the scroll operation, which provide us a method "executeScript".
1). How to Scroll up :
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,-500)", "");
2). How to Scroll Down :
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0, 500)", "");
3). How to Scroll Up to an element :
WebElement webelement = driver.findElement(By.xpath("xpath of your elements"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView(true);", webelement);
4). How to Scroll up to bottom of webpage:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
Below is the example that how to perform the above three operation in some realtime application :
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;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Scroll_demo
{
WebDriver driver;
@BeforeTest
public void navigate() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\Complete selenium\\ChromeDriver\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to("http://www.way2testing.com");
Thread.sleep(3000);
}
@Test
public void scrollTest()
{
WebElement link = driver.findElement(By.xpath(".//*[@id='post-body-751561208295527719']/div[1]/div[4]/div/table/tbody/tr[1]/td[1]/a"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
//jse.executeScript("window.scrollBy(0,500)", "");
jse.executeScript("arguments[0].scrollIntoView(true);", link);
link.click();
}
}
Thanks :) import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Scroll_demo
{
WebDriver driver;
@BeforeTest
public void navigate() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\Complete selenium\\ChromeDriver\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to("http://www.way2testing.com");
Thread.sleep(3000);
}
@Test
public void scrollTest()
{
WebElement link = driver.findElement(By.xpath(".//*[@id='post-body-751561208295527719']/div[1]/div[4]/div/table/tbody/tr[1]/td[1]/a"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
//jse.executeScript("window.scrollBy(0,500)", "");
jse.executeScript("arguments[0].scrollIntoView(true);", link);
link.click();
}
}
No comments:
Post a Comment