我正在使用 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/