我正在写一个Selenium测试用例 . 这是我用来匹配数据表中所有“修改”按钮的xpath表达式 .
//img[@title='Modify']
我的问题是,如何通过索引访问匹配的节点集?我试过了
//img[@title='Modify'][i]
和
//img[@title='Modify' and position() = i]
但两者都不起作用..我也试过XPath checker(一个firefox扩展) . 已经找到了13场比赛,然后我完全不知道我将如何选择其中一场比赛.. Or does XPath support specified selection of nodes which are not under same parent node?
5 回答
This is a FAQ :
means :文档中的所有
someName
元素,即父元素的第三个someName
子元素 - 可能有许多这样的元素 .What you want is exactly the 3rd someName element :
Explanation :
[]
的优先级(优先级)高于//
. 当您需要指定所选节点列表的第N个节点时,请记住始终将//someName
类型的表达式放在括号中 .XPath中没有
i
.要么使用文字数字:
//img[@title='Modify'][1]
或者动态构建表达式字符串:
'//img[@title='Modify']['+i+']'
(但请记住,动态XPath表达式在XSLT中不起作用) .是的:
(//img[@title='Modify'])[13]
这个
//img[@title='Modify'][i]
表示“任何<img>
, Headers 为'Modify',子元素名为<i>
. ”是的缩写
因此返回同一父节点下的第i个节点 .
你要
(// * [@ attribute ='value'])[index]查找元素的目标,同时在其中找到多个匹配项
xpath中没有
i
并不完全正确 . 您仍然可以使用count()
来查找索引 .请考虑以下页面
该页面有2个表,有6列,每列有唯一的列名,6行有可变数据 . 最后一行在两个表中都有
Modify
按钮 .假设用户必须根据 Headers 从第一个表中选择第4个
Modify
按钮使用xpath
//th[.='Heading 4']/ancestor::thead/following-sibling::tbody/tr/td[count(//tr/th[.='Heading 4']/preceding-sibling::th)+1]/button
count()
运算符在这样的情况下派上用场 .Logic:
使用
//th[.='Heading 4']
查找Modify
按钮的 Headers使用
count(//tr/th[.='Heading 4']/preceding-sibling::th)+1
查找 Headers 列的索引使用
//th[.='Heading 4']/ancestor::thead/following-sibling::tbody/tr/td[count(//tr/th[.='Heading 4']/preceding-sibling::th)+1]
获取相应标头的行使用
//th[.='Heading 4']/ancestor::thead/following-sibling::tbody/tr/td[count(//tr/th[.='Heading 4']/preceding-sibling::th)+1]/button
从提取的节点列表中获取Modify
按钮