我想使用xpath根据另一个元素的位置选择一个html元素。例如:
<table>
<tr>
<th>
Col1
</th>
<th>
Col2
</th>
<th>
Col3
</th>
<th>
Col4
</th>
</tr>
<tr>
<td>
Value1
</td>
<td>
Value2
</td>
<td>
Value3
</td>
<td>
Value4
</td>
</tr>
</table>
在此示例中,我要让td在tds集合中与具有内容Col2的th处于相同位置。
我可以找到它的位置
//th[contains(.,'Col2')]
我想避免这样做
//td[2]
有什么办法可以将两者联系起来吗?
这是一种可能的方式:
//td[
position() = count(//th[contains(.,'Col2')]/preceding-sibling::th)+1
]
XPath返回td
的位置等于的位置th
。的位置th
通过计算前置兄弟数来计算th
,因为从1的XPath位置索引开始1,。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句