Xpath to select next parent of the current node

1.8k views Asked by At

if tr contains class="productnamecolor colors_productname" i want to select next tr which contains the price details. so i use :

.//a[@class="productnamecolor colors_productname"]/parent::node()/following-sibling::tr

But didn't work. What is wrong with this expression?

HTML :

<tr> 
 <td valign="top" width="100%">
  <a href="unclesamsretailoutlet.com/Trouser-Suspenders-8440015269920-p/…; class="productnamecolor colors_productname" title="Trouser Suspenders, 2323">Trouser Suspenders</a> 
  </td>
</tr>

thanx in advance.

2

There are 2 answers

5
Michael Kay On BEST ANSWER

The parent of your <a> element is a td element, and the td element doesn't have a following-sibling - certainly not a following sibling that is a tr. If you want the next row in the table, use

.//a[@class="..."]/ancestor::tr[1]/following-sibling::tr[1]

or

.//tr[descendant::a/@class="..."]/following-sibling::tr[1]
0
Saurabh Gaur On

If you want to select just next tr after <a class="productnamecolor colors_productname"> simply use following two ways :-

  • using following axis :

    (.//a[@class="productnamecolor colors_productname"]/following::tr)[1]
    
  • using preceding axis :

    (.//tr[preceding::a[@class="productnamecolor colors_productname"])[1]
    

Hope it helps...:)