如何通过xpath获取元素的索引? [英] How to get element's index by xpath?
本文介绍了如何通过xpath获取元素的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的下一个结构是:
<div id='list'>
<div class='column'>aaa</div>
<div class='column'>bbb</div>
...
<div class='column'>jjj</div>
</div>
我想知道是否存在使用XPath的方法,并且可以写一些查询来获得
I was wonder if there is a ways to use XPath, and to write some query were I can get the index of the requested element within the "list" element.
我的意思是我要询问 class ='column'
,其中文本值为 aaa
,我将得到 0
或 1
...
I mean that I'll ask for location of class='column'
where the text value is aaa
and I'll get 0
or 1
...
谢谢
推荐答案
您可以只在要查找的元素之前计算div元素:
You could just count the div elements preceding the element you're looking for:
count(div[@id = 'list']/div[@id = 'myid']/preceding-sibling::div)
这篇关于如何通过xpath获取元素的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文