1

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>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
Darrell
  • 11
  • 1

1 Answers1

2

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.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thank you for the comment. I tried that xpath and I am still getting a Null value instead of the 'Bankruptcy' selection. Any more ideas? Thanks – Darrell May 15 '17 at 12:34
  • **1.** Establish for yourself that the XPath I provided works for the exact segment of HTML that you've provided. (It does.) **2.** Carefully investigate how the sample of HTML you've provided differs from your actual HTML or how the way you're applying the XPath is wrong. (Your posted problem does not represent your actual problem.) – kjhughes May 15 '17 at 12:39