5

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

kjhughes
  • 98,039
  • 18
  • 159
  • 218
user3303266
  • 341
  • 1
  • 7
  • 19

2 Answers2

3

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
  • 86,209
  • 12
  • 77
  • 125
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
  • 98,039
  • 18
  • 159
  • 218