18

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
  • 2,848
  • 7
  • 42
  • 70

1 Answers1

40

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

brandizzi
  • 24,902
  • 7
  • 100
  • 148
Tarun Lalwani
  • 133,941
  • 8
  • 173
  • 238
  • Excellent! Thanks a lot! – kroe761 Sep 01 '17 at 15:35
  • 8
    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