1

I am trying to match text Test number 3 using xpath in selenium.

<span>
    <br> Test number 1
    <br> Test number 2
    <br> Test number 3
</span>

I can get span element and then get last textnode from list similarly to /span//text()[3]

But can I check if Test number 3 exists directly via xpath something like below

'//span[contains(text()," Test number 3")]'

This doesn't work, only part work is '//span[contains(text()," Test")]'

But can I match with Test number 3

user2661518
  • 2,677
  • 9
  • 42
  • 79

1 Answers1

3

You need to apply the "contains" check to . - a string value of the span node, text() would just match the first text node inside the span:

.//span[contains(normalize-space(.), "Test number 3")]
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • not sure why but `normalize-space()` worked and not just `.` i.e. `//span[contains(normalize-space(), "Test number 3")]` – user2661518 Jan 04 '18 at 23:08
  • 2
    @user2661518: If `//span[contains(., "Test number 3")]` didn't select the span in your example, then (1) your real case is actually different (for example, there could be multiple spaces in between `Test` and `number` that `normalize-space()` is collapsing), (2) you made a mistake in your test, or (3) you're using a broken XPath implementation. – kjhughes Jan 04 '18 at 23:50