如何在 behat selenium 中获取 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'",));
   }

给出错误

invalid selector: The result of the xpath expression "//html//meta[@name ="description"]/@content" is: [object Attr]. It should be an element. (Session info: chrome=51.0.2704.103) (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.10.5 x86_64) (WARNING: The server did not provide any stacktrace information)

使用“@content”将 return 属性内容,当 Behat 转换为 xpath 时,这可能 return 不是有效的选择器。

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

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

另请注意,findAll returns array 而不是单个元素,您可以使用 find 找到与给定选择器匹配的第一个元素。

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

在你的情况下,你可以有类似的东西:

$found->getAttribute('name');

$found->getText();