Highlight WebElements feature is not available for Selenium Webdriver.
In Selenium there is no any Native movements hence its very hard for a selenium user to recognize where the selenium clicks or to know currently on which elements its being executed.
There is no any direct API available in WebDriver to perform this operation,But, we can achieve this using the JavaScript Executor.
Below code will achieve highlighting elements during WebDriver execution
In This Program
- Navigating to http://www.automationplace.blogspot.com
- highlight " View my profile link"
- highlight " Prashanth kk" link and click on that.
Step 1:
First we need to create a separate class file.which will hold the highlighting function.
New - > Class -> highlightclass
Source Code for highlightclass.java
import
org.openqa.selenium.JavascriptExecutor;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
public class highlightclass {
public void
highlightElement(WebDriver driver, WebElement element) throws InterruptedException {
JavascriptExecutor
js=(JavascriptExecutor)driver;
for (int i = 0; i < 3; i++) {
js.executeScript("arguments[0].setAttribute('style',
arguments[1]);",
element, "color: green;
border: 5px solid green;");
Thread.sleep(2000);
js.executeScript("arguments[0].setAttribute('style',
arguments[1]);",
element, "");
}
}
}
In the above example am using JavascriptExecutor and highliting the element in green color
color: green; border: 5px solid green;
Step 2 :
Create another class to Write automation Script and call highlightElement function
New - > Class -> funtionclass
funtionclass.java
Source Code :
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 functionclass {
public static void main(String[] args) throws InterruptedException
{
// TODO Auto-generated
method stub
//Create object for
Class method
method method=new method();
WebDriver
driver=new FirefoxDriver();
driver.get("http://www.automationplace.blogspot.com");
WebElement
element=driver.findElement(By.xpath("//*[@id='Profile1']/div/a[2]"));
method.highlightElement(driver, element);
WebElement
aboutlink=driver.findElement(By.xpath("//*[@id='Profile1']/div/dl/dt/a"));
method.highlightElement(driver, aboutlink);
aboutlink.click();
driver.quit();
}
}
Here am highlighting two webelements using highlightclass function
1. Highlight " View my profile link"
2. Highlight " Prashanth kk" link and click on that.
Hope you Like this post.