How to highlight an Element in Selenium WebDriver
package pkg_seleniumwebdriverbestpractices;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.internal.Locatable;
public class ScrollToElement {
public static WebDriver driver;
/**
* @param args
* @throws InterruptedException
*/
public static void highLightElement(WebElement element){
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].setAttribute('style','background: yellow; border: 2px solid red;');", element);
}
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.get("http://erail.in/");
driver.manage().window().maximize();
highLightElement(driver.findElement(By.id("txtStationFrom")));
driver.findElement(By.id("txtStationFrom")).clear();
driver.findElement(By.id("txtStationFrom")).sendKeys("MS" + Keys.TAB);
highLightElement(driver.findElement(By.id("txtStationTo")));
driver.findElement(By.id("txtStationTo")).clear();
driver.findElement(By.id("txtStationTo")).sendKeys("TPJ" + Keys.TAB);
WebElement lastTrain = driver.findElement(By.xpath("//div[@id='divTrainsList']/table/tbody/tr[32]/td[2]/a"));
Coordinates coordinate = ((Locatable)lastTrain).getCoordinates();
coordinate.inViewPort();
highLightElement(lastTrain);
Thread.sleep(5000L);
driver.close();
}
}
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.internal.Locatable;
public class ScrollToElement {
public static WebDriver driver;
/**
* @param args
* @throws InterruptedException
*/
public static void highLightElement(WebElement element){
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].setAttribute('style','background: yellow; border: 2px solid red;');", element);
}
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.get("http://erail.in/");
driver.manage().window().maximize();
highLightElement(driver.findElement(By.id("txtStationFrom")));
driver.findElement(By.id("txtStationFrom")).clear();
driver.findElement(By.id("txtStationFrom")).sendKeys("MS" + Keys.TAB);
highLightElement(driver.findElement(By.id("txtStationTo")));
driver.findElement(By.id("txtStationTo")).clear();
driver.findElement(By.id("txtStationTo")).sendKeys("TPJ" + Keys.TAB);
WebElement lastTrain = driver.findElement(By.xpath("//div[@id='divTrainsList']/table/tbody/tr[32]/td[2]/a"));
Coordinates coordinate = ((Locatable)lastTrain).getCoordinates();
coordinate.inViewPort();
highLightElement(lastTrain);
Thread.sleep(5000L);
driver.close();
}
}
Comments
Post a Comment