Reputation: 26599
I have the following piece of HTML on a page:
<li class="underline group">
<label class="p-left">P</label><span class="p-right">1111</span>
</li>
The following selenium commands work fine:
<tr>
<td>storeText</td>
<td>//li[2]/label</td>
<td>tmp</td>
</tr>
<tr>
<td>echo</td>
<td>${tmp}</td>
<td></td>
</tr>
The following should work but doesn't:
<tr>
<td>storeText</td>
<td>//li[2]/span</td>
<td>tmp</td>
</tr>
<tr>
<td>echo</td>
<td>${tmp}</td>
<td></td>
</tr>
The only solution I have found is //li[2]/span[@class='p-right']
but is this the case? Why doesn't //li[2]/span
work by default?
Upvotes: 0
Views: 292
Reputation: 28004
The first thing I would look for is, is there an earlier //li[2]/span
in the document, before the <li class="underline group">...<span>
that you're looking for, and the earlier span
doesn't have the class="p-right", and also has no text content? So ${tmp}
is given the text value of that first selected //li[2]/span
, and not the one you're trying to select? That would explain the results you're seeing, including your observation that //li[2]//span
also "does not work." Have you tried evaluating //li[2]/span
in a browser debugger to see which <span>
it selects first?
If that doesn't expose an explanation ... Unfortunately it's come up a few times in my experience that Selenium doesn't quite implement the XPath spec correctly. I don't know why and I agree it's frustrating. However, I think it's helpful to know that it's likely a problem with Selenium in this case, rather than a faulty understanding of XPath on your part.
If you don't find an explanation like the one outlined in the first paragraph, you'll likely have to settle for a workaround. Thankfully, you have already found a workaround in this case, using //li[2]/span[@class='p-right']
.
Upvotes: 1