Is there any way to just search the whole html document for a piece of text without worrying about tags, classes etc?
Asked
Active
Viewed 5,355 times
2 Answers
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
-
1Only limitation with this is that the string must be contained in a single text node: so no internal markup. – Michael Kay May 16 '17 at 15:30
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).