• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Basic Selenium / How to highlight elements Selenium Webdriver using JavaScript

How to highlight elements Selenium Webdriver using JavaScript

March 28, 2015 by Mukesh Otwani 32 Comments

Highlight Element in Selenium Webdriver

In Automation, testing sometimes element highlighter plays very important role. It helps us to track our execution flow which step is being processed. Some tools like QTP, Sahi etc. you will get this inbuilt feature. For Selenium, we have to write small code, which simply highlight element based on our parameter values. let’s get started and see Highlight element Selenium using CSS values.

 

Highlight element Selenium

 

In Selenium, we can use JavascriptExecutor (interface) to execute Javascript code into webdriver.

 

I have published video on the same which covers the same.

 

In this post we, will execute Javascript which will highlight the element

Let’s implement the same this will highlight the user name field.

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 Highlight {

 public static void main(String []args){
  
 WebDriver driver=new FirefoxDriver();
  
  driver.manage().window().maximize();
  
  driver.get("http://www.facebook.com");
  
 // Create the  JavascriptExecutor object
  JavascriptExecutor js=(JavascriptExecutor)driver; 
  
 // find element using id attribute
  WebElement username= driver.findElement(By.id("email"));  
  
 // call the executeScript method
  js.executeScript("arguments[0].setAttribute('style,'border: solid 2px red'');", username);
 }
  
}

In above program it, will highlight username field. Now we can not write every time the same code to highlight element so we will create reuse method and when we have to highlight element we will call the method.

 

Create highlight element method for reuse

public static void highLightElement(WebDriver driver, WebElement element)
{
JavascriptExecutor js=(JavascriptExecutor)driver; 

js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element);

try 
{
Thread.sleep(500);
} 
catch (InterruptedException e) {

System.out.println(e.getMessage());
} 

js.executeScript("arguments[0].setAttribute('style','border: solid 2px white');", element); 

}

 

Complete program for Highlight element Selenium

package com.bog.htmldemo;

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 aa {


public static void main(String []args){


WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("http://www.facebook.com");


// Inspect element
WebElement username= driver.findElement(By.id("email")); 

// Call reuse method
highLightElement(driver,username);

}

// Element highlighter code
public static void highLightElement(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); 

}

}

You can also create a separate library for this and can you in other programs as well.

Try from your side and let me know if you finding some issue.

Please share with others as well. Keep visiting. Have a nice day 🙂

Filed Under: Basic Selenium Tagged With: Element highlighter in Selenium, Highlight element Selenium in Selenium Webdriver, Highlight Object in Selenium Webdriver

Reader Interactions

Comments

  1. Ganesh says

    April 24, 2023 at 6:39 PM

    I didn’t get u, can you make video on this. SO that we can use it in utilities

    Reply
    • Mukesh Otwani says

      July 24, 2023 at 1:03 AM

      Sure Ganesh will do that

      Reply
  2. Trupti says

    December 23, 2020 at 6:53 PM

    Hi in last few seconds of video you said customized method so we don’t have to call it again and again..So please can you share that?

    Reply
    • Mukesh Otwani says

      December 27, 2020 at 8:48 AM

      Hi Trupti,
      You can add it into the utility method while calling driver.findElement for the required locator. This has to integrate into frameowrk.

      Reply
  3. Mouse says

    May 13, 2020 at 8:21 PM

    The example you have above to highlight the username throws an exception when I tried it. I fixed the issue by changing it to:
    js.executeScript(“arguments[0].setAttribute(‘style’,’border: solid 2px red’)”, element)

    Reply
    • Mukesh Otwani says

      May 13, 2020 at 9:10 PM

      Hi Mouse,

      Thanks for bringing it up. I’ll make necessary changes into code fragment.

      Reply
  4. Ajay says

    February 9, 2017 at 11:15 PM

    Hi Mukesh,
    thanks.. its nice and quite handy!!
    in the top windows, i guess one of the single quotes has got misplaced in the below line of code. the closing quote for ‘style’ is missing, and after ‘red’ there is an extra one.
    js.executeScript(“arguments[0].setAttribute(‘style,’border: solid 2px red”);”, username);

    Reply
    • Mukesh Otwani says

      February 10, 2017 at 7:15 PM

      Hi Ajay,

      Thanks for pointing out this. This will others too.

      Reply
  5. Sabbir says

    February 6, 2017 at 6:06 AM

    Very handy code. Great work.

    Reply
    • Mukesh Otwani says

      February 6, 2017 at 11:27 AM

      Hi Sabbir,

      Thanks for your comments.

      Reply
  6. Automation help says

    February 3, 2017 at 10:32 AM

    In Highlight element Selenium video you mentioned about creating customize method for finding, highlighting Can you please ping me that code as in above case i have to call method for ever element in my frame work.

    Reply
    • Mukesh Otwani says

      February 3, 2017 at 5:22 PM

      Hi there,

      Create a method which accepts can accept id or xpath or etc as arguments and make its return type as WebElement for same method. Once you get WebElement as return type then execute driver operation inside your script.

      Reply
      • Automation Helper says

        February 5, 2017 at 8:56 PM

        How to make this work for POM?

        Reply
        • Mukesh Otwani says

          February 6, 2017 at 11:40 AM

          Hi there,

          I didn’t get you. Please explain…

          Reply
  7. Aravind says

    January 25, 2017 at 4:21 PM

    Hi Mukesh
    In Highlight element Selenium video you mentioned about creating customize method for finding, highlighting and wait until element is found. Please ping me the video link.

    Reply
    • Mukesh Otwani says

      January 25, 2017 at 4:52 PM

      Hi Aravind,

      Please find the link for same
      Highlight element–>> https://youtu.be/ND_9guCggdA
      Explicit Wait –>> https://youtu.be/rlUzo_w11Ao
      Implicit Wait–>> http://learn-automation.com/implicit-wait-in-selenium-webdriver/
      Fluent Wait –>> https://youtu.be/xmgWAaFtoFA

      Reply
  8. Liam says

    November 28, 2016 at 5:26 PM

    Do i have to use Webelement in front of every driver.findElement to use this,
    do i have to keep repeating calling the function?

    Reply
    • Mukesh Otwani says

      November 28, 2016 at 11:28 PM

      Hi Liam,

      We use in the same way as I shown in video. We created a library and we are calling for every element.

      Reply
      • Azhar says

        February 9, 2017 at 4:28 PM

        Hi Mukesh,
        How i can highlight partial text from link using this method, as this method will work for element?

        Reply
        • Mukesh Otwani says

          February 9, 2017 at 8:09 PM

          Hi Azhar,

          As per my knowledge, It is not possible to highlight partial text. Because WebElement will cover whole text.

          Reply
  9. venkat says

    October 28, 2016 at 11:51 AM

    Thanks mukesh ji.. with your blogs and videos i am also becoming very passionate about selenium automation. Thanks a ton for that.

    Reply
    • Mukesh Otwani says

      November 5, 2016 at 11:02 PM

      Hey Venkat,

      I am really glad to know that you got passion and interest. Keep learning and let me know if any help.

      Reply
  10. sheshajee says

    September 22, 2016 at 8:00 PM

    hi Mukesh,
    Can you reply the video link how can i write one time method which will highlight at every time of execution

    Reply
    • Mukesh Otwani says

      September 23, 2016 at 2:44 AM

      Hi Sheshajee,

      Yes you do that. Try to add highlighter code in below method as well.

      Reply
  11. zeeshan says

    September 19, 2016 at 12:24 AM

    Hi Mukesh,

    I am not able to understand the significance of below line of code used:

    js.executeScript(“arguments[0].setAttribute(‘style’, ‘background: yellow; border: 2px solid red;’);”, ele);

    Not sure where this “ele” is coming from as we have declared our webElement with the reference “element”

    Reply
    • Mukesh Otwani says

      September 19, 2016 at 12:38 AM

      Hi Zeeshan,

      Correct, You found the defect. It should be element.

      I have updated the document as well. Hope this highlighter worked for you.

      Reply
  12. Nikhil says

    September 14, 2016 at 11:24 PM

    Mukesh how do we understand Java Script, because unless we know syntax of it, its difficult to understand the above script.

    Post is really good

    Reply
    • Mukesh Otwani says

      September 15, 2016 at 4:39 PM

      Hi Nikhil,

      For Selenium you dont have to learn complete java script but still if you want to learn then w3school is best. http://www.w3schools.com/js/

      Reply
  13. Gautham says

    September 5, 2016 at 6:00 AM

    Hello Mukesh, Thanks for sharing this post. Its really helpful. I tried with IE 11 its not working any idea why?

    Reply
    • Mukesh Otwani says

      September 6, 2016 at 3:51 PM

      Yes Gautham it will work

      Reply
  14. Nur Islam says

    March 16, 2016 at 7:19 AM

    Excellent post.

    Reply
    • Mukesh Otwani says

      March 20, 2016 at 7:33 PM

      Thanks Nur

      Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

Video Player
https://www.youtube.com/watch?v=w_iPCT1ETO4
00:00
00:00
31:21
Use Up/Down Arrow keys to increase or decrease volume.

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?