How to highlight element using selenium webdriver

As we have seen in my previous posts, we can Get Page Title, Get Domain NameGenerate Alert using java script executor interface of selenium webdriver software testing tool. Using webdriver, we can perform many different actions on software web page but for that you should be aware about this kind of things. We can execute any other java scripts too using this interface of webdriver.

Let me give you one more practical example for the same. Sometimes if you want to highlight element of software web application page then you can use javascript executor. If you remember, we were using "highlight" command in selenium IDE to highlight any element of web page.

If you VIEW WEBDRIVER EXAMPLE ON THIS POST, i have created function named HighlightMyElement and written few syntax of javascript executor. Now if i want to highlight any element of software web page then simply i need to call that function as shown in bellow example. You can change your preferred highlighting color in that example.

Copy bellow given @Test method part of generate alert using javascript example and replace it with the @Test method part of example given on THIS PAGE(Note : @Test method is marked with pink color in that linked page).


HighlightMyElement(driver.findElement(By.xpath("//input[@name='fname']")));
driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("My Name");
HighlightMyElement(driver.findElement(By.xpath("//button[@onclick='myFunction()']")));
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton")));
HighlightMyElement(driver.findElement(By.cssSelector("#submitButton")));
driver.findElement(By.cssSelector("#submitButton")).click();

In above example, HighlightMyElement will highlight the all 3 targeted elements on software web page by calling function.

5 comments:

  1. Hi..Could you please let us know how do we highlight element in the "HighlightMyElement" function ?

    ReplyDelete
    Replies
    1. Do you run above test? It will show you how to do It.

      Delete
  2. above script give complitiem error

    ReplyDelete
  3. This Script is not working for Checkbox and radio buttons ??

    ReplyDelete