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


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


source


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


source







All Articles