One Xpath expression does not work in selenium but works in Firefox

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

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

but when I check the same element in Firefox, it does not have $ nbsp and double quotes;

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

I used FireFinder and used xpath:

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

he can find this element.

but when I use selenium api 2.24, it cannot find this element.

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

Do you have any ideas? Thanks!

+4
source share
2 answers

Try with normalize-space() , which truncates the space characters and trailing spaces:

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

Change the following comments:

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

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

means it matches the <td> nodes if the concatenated string content of the whole <td> is less than the leading and trailing spaces starts with "Hello world"

+7
source

I would try using the contains() function. Your xpath will look like this: //td[contains(text(),'Hello world')]

0
source

Source: https://habr.com/ru/post/1501028/


All Articles