Bryan Oakley
Bryan Oakley

Reputation: 386230

what xpath 1 expression to do case-insensitive matching of multiline text?

I have some html markup that contains many labels that look like this:

<label>One two three four</label>
<label>One two three<br>four</label>

I'm trying to access the elements in a selenium test, based on the text string "One two three four". I want to be able to match both of the above examples.

My first attempt at an xpath looks like this:

//label[text()[contains(., 'One two three four')]]

This works fine for labels that don't contain a <br> in the text, but fails to find the second instance that has a <br>.

Doing a little research, it seems like normalize-space should work, but it doesn't seem to. This is the xpath I tried, which returns only the first node

//label[text()[contains(normalize-space(.), 'One two three four')]]

I also tried this, but it returns zero nodes

//label[normalize-space(text())[contains(., 'Generate')]]

So, is there a way to craft an xpath that looks for a label with the text "One two three four", even if there is a <br> element in the actual html markup?

Upvotes: 1

Views: 860

Answers (1)

thrig
thrig

Reputation: 676

Or translate() away the spaces andthenmatchonthat:

$ cat foo.html 
<label>One two three four</label>
<label>One two three<br>four</label>
$ xpquery -p HTML '//label[contains(translate(normalize-space(.), " ", ""), "Onetwothreefour")]/text()' foo.html
One two three four
One two three
four
$ 

Upvotes: 2

Related Questions