How to Highlight Element In Java Selenium Example Program?

How to Highlight Elements In Java Selenium Example Program?

package com.selenium.basics;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class HighlightElement
   public static void Highlight_Element(WebDriver driver, WebElement element)
      JavascriptExecutor js=(JavascriptExecutor)driver; 
      js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element);
      catch (InterruptedException e) 
      js.executeScript("arguments[0].setAttribute('style','border: solid 2px white');", element);
   public static void main(String[] args) throws InterruptedException
      WebDriver driver=new FirefoxDriver();
      WebElement el=driver.findElement(By.xpath(".//*[@id='email']"));
      Highlight_Element(driver, el);

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to, or You can join me on Linkedin.

Leave a Comment