Find web element by using Sibling from Xpath

Automation

Written by:

1/ Preceding-sibling

It’s used to find nodes that is sibling of current node and current node is also placed at preceding of that nodes.

Example: we have 9 tag <td> which is children of <tr>

<tr>
<td tabindex="1"></td>
<td tabindex="2"></td>
<td tabindex="3"></td>
<td tabindex="4"></td>
<td tabindex="5"></td>
<td tabindex="6"></td>
<td tabindex="7"></td>
<td tabindex="8"></td>
<td tabindex="9"></td>
</tr>

If you want to get all tag <td> that is sibling of tag <td tabindex=”7″> and tag <td tabindex=”7″> is at preceding of other.

You can use Xpath: //td[preceding-sibling::td[@tabindex=’7′]]

It will return 7 element include: <td tabindex=”8″>, <td tabindex=”9″>

2/ Following-sibling

It’s used to find nodes that is sibling of current node and current node is also placed at following of that nodes.

Example: we will use sample above

if you want to get all tag <td> that is sibling of tag <td tabindex=”3″> and tag <td tabindex=”3″> is at following of other.

You can use Xpath: //td[preceding-sibling::td[@tabindex=’3′]]

It will return 2 element include: <td tabindex=”1″>, <td tabindex=”2″>

Leave a Reply

Your email address will not be published. Required fields are marked *