使用Selenium WebDriver获取网页中的meta description内容

13

想要使用webdriver获取页面meta description的内容。

例如,从以下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());

但是上面的代码对我没有起作用。

2个回答

16

您想获取属性值,所以请使用getAttribute()而不是getText()

driver.findElement(By.xpath("//meta[@name='description']"))
      .getAttribute("content")

0

你也可以尝试一下

driver.findElement(By.xpath("//*[@name='description']"))
      .getAttribute("content")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接