Chuchoo
Chuchoo

Reputation: 842

HTML xpath selecting preceding sibling of parent element

Below is the html of the application I am working on:

<td class="ui-selection-column" ng-reflect-ng-class="[object Object]" ng-reflect-ng-style="[object Object]" style="width: 38px; display: table-cell;">

<td ng-reflect-ng-class="[object Object]" style="display: table-cell;">
    <span class="ui-column-title">NAME</span>
    <span class="ui-cell-data">Name1</span>
</td>

I am able to write xpath to select span inside td element with text()=Name1.

//td[@style='display: table-cell;']/span[text()='Name1']

Now, how do I select the first td element above in context with the lower td element. I wrote below Xpath to select previous sibling of td element but is not working.

//td[@style='display: table-cell;']/span[text()='VendorContact_Name1']/preceding-sibling::td[@class='ui-selection-column']

Am I missing anything. Any suggestions?

Upvotes: 1

Views: 1580

Answers (1)

eLRuLL
eLRuLL

Reputation: 18799

This is according to your first xpath:

//td[@style='display: table-cell;']/span[text()='Name1']/../preceding-sibling::td[@class='ui-selection-column'][1]

because in your xpath you are selecting a span node, not the td, if you want to select the td that contains a span, it would be something like:

//td[@style='display: table-cell;' and span[text()='Name1']]/preceding-sibling::td[@class='ui-selection-column'][1]

Upvotes: 3

Related Questions