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 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示