doupu3211 2014-02-21 21:42 采纳率: 100%
浏览 4359
已采纳

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.
  • 写回答

2条回答 默认 最新

  • douyasihefu6214 2014-02-22 00:47
    关注

    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']
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题