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



3 comments:

  1. I tried this script with a Web Page for Registration. Only the Radio buttons and Check boxes are not selected with the color. Other controls are ok. Can you suggest an idea on how these check boxes and radio bottons can be highlighted with a color

    ReplyDelete
  2. Thank You For your Awosme Tutorials On Selenium Webdriver With Java it will Most Usefull For Begginers like me and more and who are trying to learn own from Web. Once Again Thank you For Your Effort.

    ReplyDelete