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);
      try 
      {
         Thread.sleep(1000);
      } 
      catch (InterruptedException e) 
      { 
         System.out.println(e.getMessage());
      }
      js.executeScript("arguments[0].setAttribute('style','border: solid 2px white');", element);
   }
   public static void main(String[] args) throws InterruptedException
   {
      WebDriver driver=new FirefoxDriver();
      driver.get("http://www.facebook.com");
      WebElement el=driver.findElement(By.xpath(".//*[@id='email']"));
      Highlight_Element(driver, el);
      Thread.sleep(5000);
      driver.close();
   }
}
Avatar for Softwaretestingo Editorial Board

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 softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment