Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One Xpath expression doesn't work in selenium, but works in Firefox

I have one question about xpath. There is td like this in chrome:

<td class="dataCol col02">
"Hello world(notes:there is$)nbsp;"
<a href="xxxx">[View Hierarchy]</a>
</td>

but when I inspect the same element in Firefox it doesn't have $nbsp and double quotes;

<td class="dataCol col02">
Hello world
<a href="xxxx">[View Hierarchy]</a>
</td>

I used FireFinder and use the xpath:

//td[text()='Hello world']

, it can locate that element.

but when I use selenium api 2.24, it couldn't find that element.

by.xpath("//td[text()='Hello world']")

Do you have any idea of that? Thanks!

like image 442
user2755867 Avatar asked Mar 22 '23 12:03

user2755867


1 Answers

Try with normalize-space() which trims leading and trailing whitespace characters:

//td[normalize-space(text())='Hello world']

Edit following the different comments:

here's an XPath expression that's probably better suited in the general case:

//td[starts-with(normalize-space(.), 'Hello world')]

meaning it matches <td> nodes if the concatenated string content of the whole <td>, less leading and trailing whitespace, starts with "Hello world"

like image 89
paul trmbrth Avatar answered Apr 25 '23 18:04

paul trmbrth