23

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.

kroe761
  • 3,296
  • 9
  • 52
  • 81

1 Answers1

56

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

brandizzi
  • 26,083
  • 8
  • 103
  • 158
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • Excellent! Thanks a lot! – kroe761 Sep 01 '17 at 15:35
  • 11
    To be clear, it is also worth pointing out this fragment from documentation "The normalize-space function strips leading and trailing white-space from a string, **replaces sequences of whitespace characters by a single space**, and returns the resulting string.". It is worth being careful about the **bold** fragment. This can be both useful and disturbing, depending on the situation. – Mateusz Sęczkowski May 22 '20 at 12:53