0

I have listBox with value "This is_a_test" ( there is a space after the This )

$x('//li[contains(@class,"myClass")][text()="This is_a_test"]')

When I run it I got empty list []

I tried also

$x('//li[contains(@class,"myClass")][text()="This<b></b> <b></bis_a_test"]')

What do I need to change in my expression ?

The XML

<li .... >
"This"
<b></b>
<b></b>
"is_a_test"
</li>
user1365697
  • 5,819
  • 15
  • 60
  • 96

1 Answers1

1

The browser "shows" an space but in the command line you get

xmllint --html --xpath "//li[contains(@class,'myclass')]/text()" test.html
"This"


"is_a_test"

So there are 3 new lines on the result which are also part of text() output.

Removing new lines from the html, this XPath works (reversing quotes for simplicity)

echo '<li class="myclass">"This"<b></b> <b></b>"is_a_test"</li>' | \
xmllint --html --xpath "//li[contains(@class,'myclass')][.='\"This\" \"is_a_test\"']/text()" -

Result:

"This" "is_a_test"

Please note the dot . operator instead of text().
It's not easy to represent new lines on an xpath expression. Also, you may want to check this answer for more info on the difference between dot and text().

LMC
  • 10,453
  • 2
  • 27
  • 52