Question

I am using html agility for below task.

I am wondering what can be xpath query to get all the nodes containing a string search term. It should search both attributes and innertext of elements.

<HTML>
 <BODY >
  <H1>Mr T for president</H1>
   <div class="test">We believe the new president should be</div>
   <div id="test">the awsome Mr T</div>
   <div>
    <H2>Mr T replies:</H2>
     <p>test paragraph</p>
     <p class="test">for Mr T</p>
   </div>
  </BODY>
</HTML>

say I want to get all html elements have test either in their attributes or innertext?

Was it helpful?

Solution

To find all element nodes that contain a given token in an attribute value or text node, you can use this:

//*[text()[contains(., 'token')] or @*[contains(., 'token')]]

Be aware that this will fail when the text is interrupted by other markup, for example in <p>foo<em>bar</em></p>.

OTHER TIPS

You can try this XPath to match keyword 'test' against element's inner text or attribute value :

//*[contains(text(), 'test') or @*[contains(., 'test')]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top