1

I have some HTML like this:

   <div>  Make </div>

And I want to match it based on the fact that the content of the node contains the text "Make".

Put another way "Make" is a substring of the div node's content and I want to make such a match on this node using XPath.

Terrence Brannon
  • 4,760
  • 7
  • 42
  • 61

3 Answers3

3

The obvious solution would be

//div[contains(., 'Make')]

but this will find all divs that contain the string "Make" anywhere within their content, so not only will it find the example you've given in the question but also any ancestor div of that one, or any divs where that substring is buried deep in a descendant element.

If you only want cases where that string is directly inside the div with no other intervening elements then you'd have to use the slightly more complex

//div[text()[contains(., 'Make')]]

This is subtly different from

//div[contains(text(), 'Make')]

which would look only in the first text node child of the div, so it would find <div>Make<br/>Break</div> but not <div>Break<br/>Make</div>

If you want to allow for intervening elements other than div, then try

//div[contains(., 'Make')][not(.//div[contains(., 'Make'])]
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
1

Seems like this is what you are looking for: //div[contains(text(),'Make')]

If this will not work you can try: //div[contains(.,'Make')]. This will find all divs, which contain 'Make' in any attribute.

ievche
  • 1,735
  • 14
  • 22
1

To find that node anywhere in the document, you would need this:

//div[contains(text(), "Make")]
Mark Henwood
  • 951
  • 2
  • 11
  • 9