Finding a range with specific content using xpath

I have a range like this:

<span> 
  <span>foobar</span>  
  <span>thisisatest</span> 
</span>

I am trying to use xpath to find a span with "thisisatest" in it.

I tried this:

span[text()='thisisatest']

and it does not seem to work.

+8
source share
2 answers

You are absent //at the beginning of XPath.

  • One slash will mean "the span that is a child of the root node."
  • Two slashes mean "find me any space with this text."

//span[text()='thisisatest']

+20
source

You can try the following:

span[span= "thisisatest"] 

All elements <span>containing at least one child <span>with thisisatest value.

Hope this helps!

+1
source

All Articles