[フレーム]

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

by 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 🙂

Reader Interactions

Comments

  1. Trupti says

    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?

  2. Mouse says

    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)

  3. Ajay says

    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);

  4. Automation help says

    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.

  5. Aravind says

    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.

  6. Liam says

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

  7. sheshajee says

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

  8. zeeshan says

    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”

  9. Nikhil says

    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

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.

AltStyle によって変換されたページ (->オリジナル) /