public class testHighLight {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("百度主页网址自己写上去");
WebElement button = driver.findElement(By.id("kw"));
button.sendKeys("贴吧");
driver.findElement(By.id("su")).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
List<WebElement> buttons = driver.findElements(By.xpath("//*[contains(text(),'贴吧')]"));
for(int i=0;i<buttons.size();i++){
highlight(driver,buttons.get(i));
}
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(srcFile, new File("D:\\screenshot4.png"));
} catch (IOException e) {
e.printStackTrace();
}
driver.close();
driver.quit();
}
/**
* 高亮显示页面元素
*
@param diver 浏览器驱动
* @param element 高亮显示对象元素
*/
public static void highlight(WebDriver diver, WebElement element) {
JavascriptExecutor js = (JavascriptExecutor) diver;
js.executeScript("element = arguments[0];" +
"original_style = element.getAttribute('style');" +
"element.setAttribute('style', original_style + \";" +
"background: yellow; \");" +
"setTimeout(function(){element.setAttribute('style', original_style);}, 1000);", element);
}
}
效果图:
