Showing posts with label highlight element. Show all posts
Showing posts with label highlight element. Show all posts

Tuesday, May 20, 2014

How to Highlight Element with Selenium Webdriver using Java Script


We can highlight the Element using below method - 


 for (int i = 0; i < 2; i++) {
       
    WebElement element = driver.findElement(locator);
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].setAttribute('style', arguments[1]);",
                element, "color: blue; border: 3px solid red;");
        js.executeScript("arguments[0].setAttribute('style', arguments[1]);",
                element, "");
       
    }





We can also create the function to call the same frequently.

public void highlightElement(WebElement element) {
   
    for (int i = 0; i < 2; i++) {
       
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].setAttribute('style', arguments[1]);",
                element, "color: blue; border: 3px solid red;");
        js.executeScript("arguments[0].setAttribute('style', arguments[1]);",
                element, "");
       
    }

}



We can call above function as - 

WebElement element  = driver.findElement(By.xpath(locator);
 highlightElement(element  );