Selenium WebDriver Xpath当前元素属性报告无效的Xpath异常 [英] Selenium WebDriver Xpath current element attribute reports invalid Xpath exception

查看:1136
本文介绍了Selenium WebDriver Xpath当前元素属性报告无效的Xpath异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有喜欢的html

<html>
<body>
  <div class='open'>
    <h1>Title</h1>
    <div>Opened</div>
  </div>
</body>
</html>

在Selenium WebDriver 3测试中,我试图使用以下xpath选择div.open元素:

//h1/../.[contains(@class, 'open')]

在c#中的以下命令中:

driver.FindElement(By.XPath("//h1/../.[contains(@class, 'open')]"));

这将导致

OpenQA.Selenium.InvalidSelectorException:无效的选择器: 由于以下错误,无法使用xpath表达式//h1/../.[contains(@class,'open')]定位元素: 无法对文档"执行评估":字符串'//h1/../.[contains(@class,'open')]'不是有效的XPath表达式.

在Firefox控制台中通过相同的Xpath搜索成功找到了元素.

任何想法为何WebDriver都认为此xpath无效?

注意:我的示例当然是简化的

解决方案

我要说的可能是 xpathtester .但正如您所注意到的,其他实现可能会支持它.

从变通办法开始,有许多XPath替代品,它们与您尝试的原始XPath等效或接近.如上所述,最简单的方法是将.扩展为其未缩写的表达式.就个人而言,如果可能的话,我宁愿不要掉下树然后返回以返回父元素.相反,只要停在我们要返回的父元素上,然后检查谓词中的子元素即可:

//*[contains(@class, 'open')][h1]

I have html like

<html>
<body>
  <div class='open'>
    <h1>Title</h1>
    <div>Opened</div>
  </div>
</body>
</html>

And in my Selenium WebDriver 3 tests I am trying to select the div.open element using the following xpath:

//h1/../.[contains(@class, 'open')]

In the following command in c#:

driver.FindElement(By.XPath("//h1/../.[contains(@class, 'open')]"));

Which results in

OpenQA.Selenium.InvalidSelectorException : invalid selector: Unable to locate an element with the xpath expression //h1/../.[contains(@class, 'open')] because of the following error:: Failed to execute 'evaluate' on 'Document': The string '//h1/../.[contains(@class, 'open')]' is not a valid XPath expression.

Searching by the same Xpath in Firefox console successfully locates the element.

Any ideas why WebDriver considers this xpath invalid?

Note: my example is of course simplified

解决方案

I would say that's probably down to the specification doesn't explicitly state whether predicate after abbreviated step i.e . and .. should be allowed, neither the specification ever mention an example involving predicate after abbreviated step.

So some XPath implementations just don't support it. One of these, I noticed, is XPath implementation in .NET framework. Other implementations explicitly disallow it, and even provide useful suggestion for the user to replace . or .. in their XPath with the equivalent unabbreviated expression, in order, self::node() or parent::node(), for example, the implementation used by xpathtester. But as you noticed, other implementations might support it.

As of workaround, there are many alternatives XPath which equivalent or close enough to you original attempted XPath. The easiest is to expand the . to its unabbreviated expression, as I mentioned above. Personally, I prefer not to go down the tree and then back up to return a parent element, if possible. Instead, just stop at the parent element which we want to return, and check for the child in predicate :

//*[contains(@class, 'open')][h1]

这篇关于Selenium WebDriver Xpath当前元素属性报告无效的Xpath异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆