Mink中的XPath错误:无法对'Document'执行'evaluate':结果不是节点集,因此无法转换为所需的类型

我正在使用Mink和Selenium 2驱动程序和Behat进行一些验收测试,在大多数情况下,一切都进行得很顺利。

但是,我试图使用 XPath 将基于 data-* 属性的元素作为目标,结果测试无法使用该元素。

我在这两个扩展中使用了XPathHelper和FirePath以及XPath签出:

//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.

Your XPath expression is a totally valid expression – it will find all @data-search-id attributes and return true if one of them is 'images', otherwise false.

But you want to click an item, and obviously clicking a boolean value is rather difficult. Query for the item fulfilling the condition instead (thus, move the comparison into a predicate):

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

Additionally, I'd remove the //html. The HTML node must be the root node anyway, so /html would have been fine (no reason for searching it in all subtree). As you're searching for an arbitrary descendent of it, and this will not be the root node (as <html/> is), omitting it completely does not change the meaning of the XPath expression.

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

I think the XPath you're looking for is:

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