Wednesday 28 February 2018

How to Highlight Element in Selenium

When we click on any element in automation script then sometimes script is so fast that we can not recognize that which element is clicked.

So, we can do one thing that before click on any element we can highlight that element then click on that element. So, that we can recognize that which element is clicked.

We can not directly highlight element using selenium. We need to use Java script for highlight that element. If we want to execute java script then selenium driver directly can not execute java script. We have to use 'JavascriptExecutor' for execute java script with web driver.

I have created two function one for only highlight element and second one is for highlight element and lick on that element.

First function actually works like blinking on that element.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public void mHighlightElement(WebDriver driver, WebElement vElement)
    {     
     try
     {
      //Creating JavaScriptExecuter Interface
      JavascriptExecutor js = (JavascriptExecutor)driver;      
      for (int i=0; i<3; i++)
      {       
              js.executeScript("arguments[0].style.border='4px groove blue'", vElement);
              Thread.sleep(1000); // wait for see blinking
              js.executeScript("arguments[0].style.border=''", vElement);
      }
       
     }
     catch (Exception E)
     {
   System.out.println("Error in Highlight element");
  }
    }

Second Function


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public void mHighlightAndClick(WebDriver driver, WebElement vElement)
    {
     JavascriptExecutor js = (JavascriptExecutor)driver;
     try
     {
       js.executeScript("arguments[0].style.border='4px groove blue'", vElement);
          Thread.sleep(1000);
          
          vElement.click();                             
     }
     catch(Exception E)
     {
      System.out.println("Error in Highlight and click on element");
     }
    }


Please comment if you have any question.


No comments:

Post a Comment

Popular