Darrell
Darrell

Reputation: 11

XPath matching the class of a parent

I am having difficulty with writing the xpath for this bit of code. I am trying to write my XPath using @class instead of @id since the ids could change from product to product.

Here is the xpath I created:

/td[@class='dataCell  ']/table/tbody/tr[2]/td/span/select[@class='user-success']//option[2]/text()

Here is the segment of code I'm working with:

<td class="dataCell  " id="j_id0:j_id1:j_id6:section2:j_id34:j_id35:j_id67:j_id68:0:j_id138:j_id139:0:j_id216" colspan="1"><table>
<tbody>
<tr>
<td><select class="dropLogic user-success" id="j_id0:j_id1:j_id6:section2:j_id34:j_id35:j_id67:j_id68:0:j_id138:j_id139:0:j_id219" name="j_id0:j_id1:j_id6:section2:j_id34:j_id35:j_id67:j_id68:0:j_id138:j_id139:0:j_id219" onchange="requireSaving();"><option value="">--None--</option><option value="100% Loss">100% Loss</option>
<option value="Recoverable">Recoverable</option>
<option value="Internal">Internal</option>
<option value="Supersede">Supersede</option>
<option value="Continue Service">Continue Service</option>
<option value="Expire">Expire</option>
</select></td>
</tr>
<tr>
<td><span><select id="j_id0:j_id1:j_id6:section2:j_id34:j_id35:j_id67:j_id68:0:j_id138:j_id139:0:j_id222" name="j_id0:j_id1:j_id6:section2:j_id34:j_id35:j_id67:j_id68:0:j_id138:j_id139:0:j_id222" class="user-success"><option value="">--None--</option><option value="Bankruptcy">Bankruptcy</option><option value="Property no longer exist">Property no longer exist</option></select></span></td>
</tr>
</tbody>
</table>
</td>

Upvotes: 1

Views: 54

Answers (1)

kjhughes
kjhughes

Reputation: 111521

Main problem is a spacing issue; use normalize-space():

/td[normalize-space(@class)='dataCell']/table/tbody/tr[2]/td/span/select[@class='user-success']/option[2]/text()

to select Bankruptcy as intended.

Note, you might want to go further and apply an even more robust technique to match when there could be multiple classes now or in the future.

Upvotes: 2

Related Questions