ad

Sunday, March 27, 2016

Highlight an Element using Selenium Web Driver

Sometimes we want to make sure that we have used a right locator to identify an element. For this purpose, we can highlight that element before applying any operation on that. In this way, we will be sure that we are targeting a right element.
There is no direct way in selenium webdriver to highlight any element. But, we do have Javascript Executor class which we can use to highlight any element through javascript.

Highlight an Element using Selenium Web Driver

See the code below. Language used is C#
IWebDriver driver = new FirefoxDriver();

var element = driver.FindElementById('txtuser');

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;

js.ExecuteScript("arguments[0].style.border='3px solid red'", element);
we can go one step further and make it a function.
 public static void HighlighElement(this IWebDriver driver, IWebElement element)
        {
           IJavaScriptExecutor js = (IJavaScriptExecutor)driver;

           js.ExecuteScript("arguments[0].style.border='3px solid red'", element); 
            

        }
Please note that above is an extension method. So this function will be available across every instance of webdriver.
To use this function in our code, we can use it like this with any webdriver instance.
IWebDriver driver = new FirefoxDriver();

var element = driver.FindElementById('txtuser');

driver.HighlighElement(element);
I hope this post helps you in automating your web application.
Please do leave your feedback in comments section.

5 comments:

  1. As disussed above software testing is one of the important field in detecting the bugs. In Software testing selenium training institute in Chennai we are enabled to identify the errors with advanced level of selenium course and can match the actual results with that of expected results.

    ReplyDelete
  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.


    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete
  3. Wonderful post!!Thank you for sharing this info with us.
    Keep updating I would like to know more updates on this topic
    Very useful content, I would like to suggest this blog to my friends.

    software testing selenium training

    selenium course

    ReplyDelete
  4. Wonderful post!!Thank you for sharing this info with us.
    Keep updating I would like to know more updates on this topic
    Very useful content, I would like to suggest this blog to my friends.

    selenium testing training

    Selenium Courses in Chennai

    ReplyDelete