How to get the preceding element?

前端 未结 3 968
有刺的猬
有刺的猬 2020-12-14 07:21

Pages : 1 ,

相关标签:
3条回答
  • 2020-12-14 07:30
    //a[contains(text(), 'Next.')]/preceding::a[contains(text(), '171')]
    

    Explanation of xpath: Using text method along with <a> tag and then move ahead with preceding keyword to locate the element 171

    0 讨论(0)
  • 2020-12-14 07:37

    You can use this xpath:

    //a[.="Next."]/preceding::a[1]
    

    If I were to diagram it out, using an X to represent the current location, it would look like this:

    ------------------+------+------------------
    preceding-sibling | self | following-sibling
    ------------------|------|------------------
    last() ...  2   1 |  X   | 1   2  ... last()
    ------------------+------+------------------
    
    0 讨论(0)
  • 2020-12-14 07:48

    I know this is old and if you didn't know the containing element preceding the "Name." element this wouldn't be a solution for you. BUT, if you were wanting to find exactly that element and there are several "171" elements all over the page. The way to distinguish it from the rest, you could use the following.

    //p[b[contains(., 'Next.')]]//a[contains(., '171')]
    
    0 讨论(0)
提交回复
热议问题