HTML Agility pack + Select node by its inner text

c#-4.0 html-agility-pack xpath

Question

I can now discover certain nodes using their attributes and xpaths by utilizing the HTML Agility Pack. The issue is that I've been carrying out each of my projects manually (opening the website html and scanning for the nodes that have the text I need). Is it possible to choose a certain node based on its inner text? This would make it simpler to develop update scripts for webpages whose content scheme remains constant over time but whose attribute tags alter their values. I appreciate it.

1
2
8/14/2014 7:40:20 PM

Popular Answer

It would have been preferable if you had given an example HTML document, but as you haven't, let's pretend we have HTML that uses the following markup:

<body>
    <div class="foo">bar</div>
</body>

You may choose the<div> utilizing HtmlAgilityPack's attributeSelectSingleNode() and XPath as follows:

myHtmlDocument.DocumentNode.SelectSingleNode("//div[@class='foo']");

alternatively you may do the same by clicking on the inside text as follows:

myHtmlDocument.DocumentNode.SelectSingleNode("//div[.='bar']");

I hope this was helpful.

4
8/15/2014 10:39:46 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow