Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath Expression to match text correctly, but trim leading and trailing whitespace

Tags:

selenium

xpath

I need an xpath expression for my selenium tests to get this element:

<td class="label">        Order Date      </td>

but not this one:

<td class="label">    Order Dates  </td>

I tried these two:

"//*[text() = 'Order Date']"
"//*[text()[contains(.,'Order Date')]]"

But the exact match expression doesn't take the whitespace into account, and the contains expression also targets the element with an s.

like image 627
kroe761 Avatar asked Sep 01 '17 15:09

kroe761


1 Answers

You need to use the XPath's normalize-space() as in //td[normalize-space()="Order Date"]

like image 85
Tarun Lalwani Avatar answered Oct 14 '22 11:10

Tarun Lalwani