Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath find element's grandparent

Tags:

xpath

I'm a bit stuck with getting a grandparent element by Xpath. Unfortunately, my tries were unsuccessful.

HTML

<span class="fsm fwn fcg">
  <a href="bla bla">
    <abbr>
      <span class="timestampContent" id="js_19">21 mins</span>
    </abbr>
  </a>
</span>

i need to get the <a href="bla bla"> element as the grandparent for <span class="timestampContent" id="js_19">21 mins</span>

i tried something like the following: //span[@class='timestampContent' and contains(text(), 21 mins)]../.. or ../..//span[@class='timestampContent' and contains(text(), 21 mins)]

and some other options, but it didn't work as I expected.

like image 746
user1935987 Avatar asked Jun 09 '26 16:06

user1935987


1 Answers

Your first try is close, only a / before .. was missing (and missing quotes around '21 mins', which I believe was just a typo) :

//span[@class='timestampContent' and contains(text(), '21 mins')]/../..

Alternatively, you can do it the other way around i.e by selecting element that has grand child span of certain criteria :

//*[*/span[@class='timestampContent' and contains(text(), '21 mins')]]
like image 177
har07 Avatar answered Jun 11 '26 08:06

har07