如何获得硒元素中的Xpath


How to get the Xpath in behat selenium

尝试在页面中查找Xpath。这是我的密码。

   $session = $this->getSession();
   $found = $session->getPage()->findAll('xpath', '//meta[@name ="description"]/@content');
   if (is_null($found)) {
      throw new 'Exception(sprintf("Could not find meta %s='%s' with '%s'",));
   }

给出错误

无效选择器:xpath表达式的结果"//html//meta[@name="description"]/@content"是:[object Attr]。它应该是一个元素。(会话信息:chrome=51.0.2704.103)(驱动程序信息:chromedriver=2.21.371459(36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),平台=Mac OS X 10.10.5x86_64)(警告:服务器未提供任何堆栈竞争信息)

使用'@content'将返回属性content,当Behat转换为xpath时,这可能不会返回有效的选择器。

如果你需要指定该属性,你应该这样使用:

//meta[@name ="description"][@content]

另外请注意,findAll返回数组,而不是单个元素,您可以使用find来查找与给定选择器匹配的第一个元素。

如果需要获取属性的值或获取文本,可以使用getAttribute('attribute_name')或getText()等方法。

在你的情况下,你可以有这样的东西:

$found->getAttribute('name');

$found->getText();