php - Mink : Failed to execute 'evaluate' on 'Document' : The result is not a node set, 中的 XPath 错误,因此无法转换为所需的类型

标签 php xpath behat mink

我正在使用 Mink 和带有 Behat 的 Selenium2 驱动程序来运行一些验收测试,并且在大多数情况下,一切都很顺利。

但是,我正在尝试使用 XPath 来定位基于 data-* 属性的元素,并且测试会阻塞它。

我使用了 XPathHelper 和 FirePath,并且我的 XPath 在这两个扩展中 checkout :
//html//@data-search-id='images'
这似乎针对正确的元素。

但是,当我将以下内容添加到 FeatureContext.php

$el = $this->getSession()->getPage()->find('xpath', $this->getSession()->getSelectorsHandler()->selectorToXpath('xpath', "//@data-search-id='images'"));
$el->click();

我从 Behat 收到以下错误:
invalid selector: Unable to locate an element with the xpath expression
//html//@data-search-id='images' because of the following error:

TypeError: Failed to execute 'evaluate' on 'Document':
The result is not a node set, and therefore cannot be converted
to the desired type.

最佳答案

您的 XPath 表达式是一个完全有效的表达式——它将找到所有 @data-search-id如果其中之一是“图像”,则返回 true,否则返回 false。

但是你想点击一个项目,显然点击一个 bool 值是相当困难的。改为查询满足条件的项目(因此,将比较移动到谓词中):

//html//*[@data-search-id='images']

此外,我会删除 //html . HTML 节点无论如何都必须是根节点,所以 /html本来可以的(没有理由在所有子树中搜索它)。当您正在搜索它的任意后代时,这将不是根节点(如 <html/> 那样),完全省略它不会改变 XPath 表达式的含义。

//*[@data-search-id='images']

关于php - Mink : Failed to execute 'evaluate' on 'Document' : The result is not a node set, 中的 XPath 错误,因此无法转换为所需的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21945754/

相关文章:

Laravel Behat 数据库连接

javascript - 相同的选择器在常规浏览器和 Selenium 中给出不同的结果

类似于 Facebook 的 PHP/MySQL 过滤搜索

php - 将 WordPress 多站点从 Windows 移动到 Linux 后高 TTFB

xml - 使用变量中的其他节点在 XSL apply-templates 节点中排序 (XSLT 1.0)

xml - 为什么计数节点不起作用?

php - 如何在 bluemix 上安装 php 扩展?

php - Symfony2 SoftDeleteable 不适用于 QueryBuilder Delete

python - 使用Scrapy爬取本地XML文件-起始URL 本地文件地址

php - 如何运行特定的 Behat 场景