如何在selenium的xpath中使用count()函数?

Too*_*ess 3 html xml selenium xpath

使用findElements()size()方法我们可以得到计数。但我想使用 xpath 中的 count() 函数提取计数。

count 函数会返回整数值吗?

假设,我的 xpath 是(//input[@id='stack'])[3],并且有 3 个匹配节点//input[@name='hai']

我可以像下面这样修改我的 xpath 吗?

(//input[@id='stack'])[count(//input[@name=''hai])]
Run Code Online (Sandbox Code Playgroud)

kjh*_*hes 5

是的,如果

count(//input[@name='hai'])
Run Code Online (Sandbox Code Playgroud)

评估为

3
Run Code Online (Sandbox Code Playgroud)

然后

(//input[@id='stack'])[count(//input[@name='hai'])]
Run Code Online (Sandbox Code Playgroud)

将选择相同的节点

(//input[@id='stack'])[3]
Run Code Online (Sandbox Code Playgroud)

会选择。

然而,你的意图很不清楚,特别是考虑到

  • //input[@id='stack']将选择属性值为的所有input元素。通常,属性值在整个文档中是唯一的,因此通常只会选择一个元素。id'stack'id

  • (//input[@id='stack'])[count(//input[@name='hai'])]input假设id'ed 的元素至少'stackinput命名的元素一样多'hai'——这是一个奇怪的假设。