XPATH: Find a piece of text anywhere in the document
Is it possible to just search the entire HTML document for a piece of text without worrying about tags, classes, etc.
+3
user3303266
source
to share
2 answers
Yes, something like this:
//text()[contains(.,'keyword')]
Or use one of the following XPaths if you prefer to return the parent where the keyword is located:
//*[text()[contains(.,'keyword')]]
//text()[contains(.,'keyword')]/..
+2
har07
source
to share
This XPath,
contains(/,'keyword')
will return true if keyword
contained anywhere in the document string value.
Note that this may or may not match substrings concatenated between elements (i.e. <r>key<b>word</b></r>
), which may or may not be desired. If this is not desired see @ har07's answer (+1).
See also: Testing text () Nodes and String Values ββin XPath
+1
kjhughes
source
to share