想用webdriver获取页面的元描述内容。
假设,从 DOM 下面要检索文本
Test.com 以多达 22 种语言提供完整的软件解决方案,用于创建在线测试以及管理企业和专家认证计划
<script src="content/js/jquery.min.js">
<meta content="Test.com provides a complete software solution for creating online tests and managing enterprise and specialist certification programs, in up to 22 languages." name="description">
<meta content="Test.com" name="keywords">
我试过
System.out.println(driver.findElement(By.xpath("//meta[@name='description']")).getText());
但是上面的代码对我不起作用。
最佳答案
您正在尝试获取属性值,因此不要使用 getText()
,而是使用 getAttribute()
:
driver.findElement(By.xpath("//meta[@name='description']"))
.getAttribute("content")
关于java - 使用 selenium webdriver 从网页中检索元描述的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25973997/