Search code examples
xpathimport.io

Xpath to select next parent of the current node


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.


Solution

  • 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]