WebDriver找不到-是[object XrayWrapper [object Text] [英] WebDriver can't find - is [object XrayWrapper [object Text]

查看:288
本文介绍了WebDriver找不到-是[object XrayWrapper [object Text]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下路径通过xpath定位元素,但收到以下错误. xpath是否有可能找到该元素?还是驱动程序本身有问题?

Im trying to locate an element by xpath using the following path but am receiving the below error. Is it possible for xpath to find this element? Or is this an issue with the driver itself?

我希望能够区分/p [4]/text()[1]和/p [4]/text()[2]

I want to be able to differentiate between the values in /p[4]/text()[1] and /p[4]/text()[2]

.//*[@id='content']/p[4]/text()[1]

org.openqa.selenium.InvalidSelectorException: The given selector .//*[@id='content']/p[4]/text()[1] is either invalid or does not result in a WebElement. The following error occurred:

InvalidSelectorError:xpath表达式".//*[@id='content']/p[4]/text()[1]"的结果是:[对象XrayWrapper [对象文本]].它应该是一个元素.

InvalidSelectorError: The result of the xpath expression ".//*[@id='content']/p[4]/text()[1]" is: [object XrayWrapper [object Text]]. It should be an element.

这是示例的html.我正在尝试使用/text()验证文本(会话的其他问题)".

This is the html for an example. Im trying to verify the text '(extra questions for session)' with /text().

<div class="ad_field">
<label for="survey_id">Survey</label>
(extra questions for session)
<br>

</div>

推荐答案

好吧,问题在于此XPath表达式找不到您的元素,而是选择了一些文本.

Well, the problem is that this XPath Expression won't find you an element, but instead you've selected some text.

也就是说,错误消息还告诉您: The given selector [...] does not result in a WebElement.

That is, what the error message also tells you: The given selector [...] does not result in a WebElement.

您已经要求.//*[@id='content']/p[4]/text()[1],它以函数/text()结尾,该函数不选择节点,而是选择其文本.我不确定/text()[1]的作用,但假设它会选择该文本中的第二个字符.至少我认为可以安全地假设它不会神奇地将文本转换为DOM元素.

You've asked for .//*[@id='content']/p[4]/text()[1] which ends in the function /text() which selects not a node, but the text of it. I'm not exactly sure what /text()[1] does but assume it will select the second character in that text. At least I think it's safe to assume that it won't magically convert the text to an DOM Element.

WebDriver假定您的表达式生成一个DOM元素,它可以表示为WebElement.该表达式没有,因此引发了该表达式.

WebDriver however assumes that your expression results in a DOM Element it can represent as a WebElement. This expression does not and thus it throws the expression.

作为提示,您应该打开网页并在不确定时使用工具手动检查XPath.我为此使用了Firebug和FirePath插件,但是您可以自由使用任何有用的东西.

As a hint, you should open the web page and use a tool to manually check your XPath when in doubt. I use Firebug with the FirePath AddOn for this, but you're free to use whatever you find useful.

这篇关于WebDriver找不到-是[object XrayWrapper [object Text]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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