吧友们有人知道selenium可以调用浏览器的查找功能吗?或高亮方法
selenium吧
全部回复
仅看楼主
level 1
本人现在的任务是网站截屏,需要对截屏页面的关键词高亮显示,如果能试用selenium调用浏览器自身的查找(Ctrl + F)功能,就能很方便的将关键词高亮出来,求问有人知道如何操作吗?或者有什么利用selenium高亮页面关键词的方法吗?[啊]
2016年08月15日 08点08分 1
level 5
模拟键盘指令呢
2016年08月16日 00点08分 2
啊,我考虑过这个想法,模拟ctrl+F是可以把搜索框调出来,可是输入就成了问题,因为我是中文关键词搜索,那么如何模拟键盘打印中文又成了一个问题[不高兴]
2016年08月16日 01点08分
level 6
不如用find.get_text
2016年10月15日 11点10分 3
level 6
用text()全局查找
2016年10月18日 07点10分 4
level 4
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);
}
}
效果图:
2016年10月31日 06点10分 6
1