How to scroll to a particular element in Selenium WebDriver?
package pkg_selenium;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.internal.Locatable;
public class ScrollToElement {
public static WebDriver driver;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// Initialize WebDriver
driver = new InternetExplorerDriver();
// Wait For Page To Load
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
// Go to URL
driver.get("http://erail.in/");
// Maximize Window
driver.manage().window().maximize();
// Enter Station From Code
driver.findElement(By.id("txtStationFrom")).sendKeys("MS" + Keys.TAB);
// Enter Station To Code
driver.findElement(By.id("txtStationTo")).sendKeys("TPJ" + Keys.TAB);
Thread.sleep(5000L);
// Assign Object for Last Train
WebElement lastTrain = driver.findElement(By.xpath("//*[@id='divTrainsListTrainsObj']/table[1]/tbody/tr[26]/td[2]/a"));
// Scroll to Last Train
Coordinates coordinate = ((Locatable) lastTrain).getCoordinates();
coordinate.inViewPort();
Thread.sleep(5000L);
// Close Driver
driver.quit();
}
}
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.internal.Locatable;
public class ScrollToElement {
public static WebDriver driver;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// Initialize WebDriver
driver = new InternetExplorerDriver();
// Wait For Page To Load
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
// Go to URL
driver.get("http://erail.in/");
// Maximize Window
driver.manage().window().maximize();
// Enter Station From Code
driver.findElement(By.id("txtStationFrom")).sendKeys("MS" + Keys.TAB);
// Enter Station To Code
driver.findElement(By.id("txtStationTo")).sendKeys("TPJ" + Keys.TAB);
Thread.sleep(5000L);
// Assign Object for Last Train
WebElement lastTrain = driver.findElement(By.xpath("//*[@id='divTrainsListTrainsObj']/table[1]/tbody/tr[26]/td[2]/a"));
// Scroll to Last Train
Coordinates coordinate = ((Locatable) lastTrain).getCoordinates();
coordinate.inViewPort();
Thread.sleep(5000L);
// Close Driver
driver.quit();
}
}
Comments
Post a Comment