为什么XPath last()函数不能正常工作? [英] Why is XPath last() function not working as I expect?
问题描述
我正在使用Java和Selenium编写测试。我需要在另一个元素中获取最后一个元素,所以我使用 last()
函数,但问题是当我申请时它并不总是带给我最后一个元素:
I am using Java and Selenium to write a test. I need to get the last element inside another element, so I used last()
function, but the problem is that it doesn't always bring me the last one when I apply :
//a//b[last()]
到
<a>
<l>
<b>asas</b>
</l>
<b>as</b>
</a>
获取< b>为< / b>
,它带给我:
<b>asas</b>
<b>as</b>
但是当我申请时:
<a>
<b>asas</b>
<b>as</b>
</a>
它给我带来了:
<b>as</b>
推荐答案
这是XPath混淆的常见原因。首先是直截了当的部分:
This is a common source of XPath confusion. First the straightforward parts:
-
// a
全选文档中的a
元素。 -
// a // b
选择所有b
文件中的元素是
后代a
元素。
//a
selects alla
elements in the document.//a//b
selects allb
elements in the document that are descendants ofa
elements.
到目前为止正常的东西。 接下来是棘手的部分:
Normal stuff so far. Next is the tricky part:
-
选择最后一个
b
兄弟 中的元素(在a
元素下):
//a//b[last()]
这里,过滤是 b
选择标准的一部分,因为 []
的优先级高于 //
。
Here, the filtering is a part of the b
selection criteria because []
has a higher precedence than //
.
选择最后一个 b
文档中 元素 ( a
元素下方):
To select the last b
element in the document (beneath a
elements):
(//a//b)[last()]
此处, last()
是所有选定 b <的列表中的索引/ code>元素因为
()
用于覆盖默认优先级。
Here, the last()
is an index on the list of all selected b
elements because ()
is used to override the default precedence.
这篇关于为什么XPath last()函数不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!