如何使用Selenium按索引选择Xpath节点集中的指定节点? [英] How to select specified node within Xpath node sets by index with Selenium?
问题描述
我正在编写一个 Selenium 测试用例.这是我用来匹配数据表中所有修改"按钮的 xpath 表达式.
I'm writing a Selenium testcase. And here's the xpath expression I use to match all 'Modify' buttons within a data table.
//img[@title='Modify']
我的问题是,如何通过索引访问匹配的节点集?我试过
My question is, how can I visit the matched node sets by index? I've tried with
//img[@title='Modify'][i]
和
//img[@title='Modify' and position() = i]
但两者都不起作用..我也尝试过 XPath 检查器(一个 firefox 扩展).一共找到了 13 个匹配项,然后我完全不知道我该如何选择其中之一.或者XPath是否支持指定选择不在同一父节点下的节点?
But neither works.. I also tried with XPath checker(One firefox extension). There're totally 13 matches found, then I have totally no idea how am I gonna select one of them.. Or does XPath support specified selection of nodes which are not under same parent node?
推荐答案
这是一个常见问题:
//someName[3]
表示:文档中的所有 someName
元素,即其父元素的第三个 someName
子元素——可能有很多这样的元素.
means: all someName
elements in the document, that are the third someName
child of their parent -- there may be many such elements.
你想要的正是第三个 someName
元素:
What you want is exactly the 3rd someName
element:
(//someName)[3]
说明:[]
的优先级(优先级)高于 //
.当您需要指定所选节点列表的第 N 个节点时,请记住始终将 //someName
类型的表达式放在括号中.
Explanation: the []
has a higher precedence (priority) than //
. Remember always to put expressions of the type //someName
in brackets when you need to specify the Nth node of their selected node-list.
这篇关于如何使用Selenium按索引选择Xpath节点集中的指定节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!