Retrieving an element from XPath using text as basis
Terrible title, I know, but is there a way in XPath to get to a desired link by only knowing that the link is second going back from the last ellipsis?
In this instance, the desired link is /2
<div class="page">
<a href="/1">1</a>
<a href="/2">2</a>
<a href="/3">3</a>
...
<a href="/50">50</a>
<a href="/51">51</a>
<a href="/52">52</a>
</div>
In this case, it is /3.
<div class="page">
<a href="/1">1</a>
<a href="/2">2</a>
<a href="/3">3</a>
<a href="/4">4</a>
...
<a href="/50">50</a>
<a href="/51">51</a>
<a href="/52">52</a>
</div>
And, just to throw a spanner, in the works... this one is 21:
<div class="page">
<a href="/1">1</a>
<a href="/2">2</a>
<a href="/3">3</a>
...
<a href="/18">18</a>
<a href="/19">19</a>
<a href="/20">20</a>
<a href="/21">21</a>
<a href="/22">22</a>
...
<a href="/50">50</a>
<a hre开发者_运维知识库f="/51">51</a>
<a href="/52">52</a>
</div>
I've tried all sorts of ways to get at it, from writing out counts to throwing magic beans out of my window, but nothing works. And now I'm out of magic beans. :(
Any suggestions for this problem (XPath, not the magic beans!) are welcome!
/div[@class='page']/text()[normalize-space()='…'][last()]/preceding-sibling::a[2]
精彩评论