为什么我的XPath不会根据标签文本选择链接/按钮?(Why won't my XPath select link/button based on its label text?)

编程入门 行业动态 更新时间:2024-10-27 10:19:38
为什么我的XPath不会根据标签文本选择链接/按钮?(Why won't my XPath select link/button based on its label text?) <a href="javascript:void(0)" title="home"> <span class="menu_icon">Maybe more text here</span> Home </a>

因此,对于上面的代码,当我将//a编写为XPath时,它会突出显示,但是当我编写//a[contains(text(), 'Home')] ,它不会突出显示。 我认为这很简单,应该有效。

哪里是我的错?

<a href="javascript:void(0)" title="home"> <span class="menu_icon">Maybe more text here</span> Home </a>

So for above code when I write //a as XPath, it gets highlighted, but when I write //a[contains(text(), 'Home')], it is not getting highlighted. I think this is simple and should have worked.

Where's my mistake?

最满意答案

其他答案错过了这里的实际问题:

是的,您可以在@title匹配,但这并不是OP的XPath失败的原因。 是的,XML和XPath区分大小写,因此Home与home ,但是有一个Home文本节点作为a的子节点,因此如果他不相信@title存在,那么OP是正确使用Home 。

真正的问题

OP的XPath,

//a[contains(text(), 'Home')]

说要选择第一个文本节点包含子串Home所有元素。 然而,第一个文本节点只包含空格。

说明: text()选择上下文节点的所有子文本节点, a 。 当contains()被赋予多个节点作为其第一个参数时,它将获取第一个节点的字符串值,但Home出现在第二个文本节点中,而不是第一个节点。

相反,OP应该使用这个XPath,

//a[text()[contains(., 'Home')]]

它表示用字符串值包含子字符串Home 任何文本子项选择所有元素。

如果没有周围的空格,则可以使用此XPath来测试相等而不是子字符串包含:

//a[text()[.='Home']]

或者,对于周围的空白,可以使用此XPath将其修剪掉:

//a[text()[normalize-space()= 'Home']]

也可以看看:

在XPath中测试text()节点与字符串值 为什么XPath不洁净? 为什么谓词中不需要text()? XPath:点和文本之间的区别()

Other answers have missed the actual problem here:

Yes, you could match on @title instead, but that's not why OP's XPath is failing where it may have worked previously. Yes, XML and XPath are case sensitive, so Home is not the same as home, but there is a Home text node as a child of a, so OP is right to use Home if he doesn't trust @title to be present.

Real Problem

OP's XPath,

//a[contains(text(), 'Home')]

says to select all a elements whose first text node contains the substring Home. Yet, the first text node contains nothing but whitespace.

Explanation: text() selects all child text nodes of the context node, a. When contains() is given multiple nodes as its first argument, it takes the string value of the first node, but Home appears in the second text node, not the first.

Instead, OP should use this XPath,

//a[text()[contains(., 'Home')]]

which says to select all a elements with any text child whose string value contains the substring Home.

If there weren't surrounding whitespace, this XPath could be used to test for equality rather than substring containment:

//a[text()[.='Home']]

Or, with surrounding whitespace, this XPath could be used to trim it away:

//a[text()[normalize-space()= 'Home']]

See also:

Testing text() nodes vs string values in XPath Why is XPath unclean constructed? Why is text() not needed in predicate? XPath: difference between dot and text()

更多推荐

本文发布于:2023-08-05 22:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1441091.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   文本   标签   链接   XPath

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!