试图在页面中查找 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”将返回属性内容,当 Behat 转换为 xpath 时,这可能不会返回有效的选择器。
如果您需要指定该属性,您应该像这样使用它:
//meta[@name ="description"][@content]
另请注意,findAll 返回数组而不是单个元素,您可以使用 find 查找与给定选择器匹配的第一个元素。
如果需要获取属性值或获取文本,可以使用 getAttribute('attribute_name') 或 getText() 等方法。
在你的情况下,你可以有类似的东西:
$found->getAttribute('name');
或者
$found->getText();
关于php - 如何在 behat selenium 中获取 Xpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38480330/