7

Is there any way to just search the whole html document for a piece of text without worrying about tags, classes etc?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
user3303266
  • 371
  • 1
  • 9
  • 22

2 Answers2

4

Yes, something like this :

//text()[contains(.,'keyword')]

Or, use one of the following XPath if you prefer to return parent element where the target keyword resides :

//*[text()[contains(.,'keyword')]]
//text()[contains(.,'keyword')]/..
har07
  • 88,338
  • 12
  • 84
  • 137
1

This XPath,

contains(/,'keyword')

will return true if keyword is contained anywhere in the string value of the document.

Note it could match substrings conjoined across elements (i.e. <r>key<b>word</b></r>), which may or may not be desirable. If undesirable, see @har07's answer (+1).

See also: Testing text() nodes vs string values in XPath

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240