Highlight element can be done using JavascriptExecutor in selenium with java.
The following script can be used to highlight h1 element
WebDriver driver = new ChromeDriver();
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class HighlightElementExample {
public static void main(String[] args) throws InterruptedException {
// Set up ChromeDriver
WebDriver driver = new ChromeDriver();
driver.get(“https://www.example.com”);
// Locate element
WebElement element = driver.findElement(By.tagName(“h1”));
// Highlight element
highlightElement(driver, element);
// Wait a few seconds to see the highlight
Thread.sleep(2000);
// Close browser
driver.quit();
}
// Function to highlight a web element
public static void highlightElement(WebDriver driver, WebElement element) {
JavascriptExecutor js = (JavascriptExecutor) driver;
// Store original style
String originalStyle = element.getAttribute(“style”);
// Set new style (yellow background + red border)
js.executeScript(“arguments[0].setAttribute(‘style’, arguments[1]);”,
element, “border: 2px solid red; background: yellow;”);
// Optionally, revert back after a short pause
try {
Thread.sleep(500);
js.executeScript(“arguments[0].setAttribute(‘style’, arguments[1]);”,
element, originalStyle);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}