How To Highlight Element Using Selenium WebDriver

If you want to see the exact element being interacted with by Selenium, you can write code to highlight element using Selenium WebDriver. Also sometimes when we work on automation script creation in debug mode, sometimes it is helpful to see the current element being interacted with, on the webpage. To do this, we highlight element using Selenium WebDriver.

Selenium provides so many capabilities to perform test automation which makes it easy to perform any webpage-based task. This page has a working code example which can be used in your code if you want to highlight a webelement using Selenium WebDriver and Java.

If you are new to this, it would seem a little different to you that Selenium uses Javascript to handle this specific scenario.

Javascript is run with the help of JavascriptExecutor and in order to highlight the element,  we change the color of the element.  See the code below.

Example code to Highlight element using Selenium WebDriver
 * This method highlights a webelement during test execution
 * @param element This is element

public static void highlightElement(WebDriver driver, WebElement element)  {
	for (int i = 0; i <3; i++) {
		JavascriptExecutor js = (JavascriptExecutor) driver;
		js.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: red; border: 5px solid yellow;");
		try {
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block

		js.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "");

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *