Get value between html tags Xpath and HtmlAgility

c# html html-agility-pack html-parsing xpath


So Far I am trying to retrieve the text between HTML tags for a certain website....

Say for instance I need to extract out the text between these span tags how would I go about that, I am receiving an error stating "the object reference not set to an instance of an object" here is the HTML

There is also HTML Code prior to this portion here; I don't know if that should make a difference.

<div class="thumbnail-details">
    <li> … </li>
    <li class="product-title">
        <span class="thumbnail-details-grey">The Blaster Portable Wireless Speaker in Black</span>
    <li> … </li>

So far my C# code is

    HtmlWeb hw = new HtmlWeb();
        HtmlAgilityPack.HtmlDocument htmlDoc = hw.Load(@"");
        if (htmlDoc.DocumentNode != null)
            foreach (HtmlNode text in htmlDoc.DocumentNode.SelectNodes("//span[@class='thumbnail-details-grey']/text()"))

Can I get some help here, I want to extract out "The Blaster Portable Wireless Speaker in Black".

1/2/2019 10:41:10 AM

Accepted Answer

Your code works just fine, but you'll have to load the right page to get it to work. The page you are loading uses an ajax request to load the results you see in your browser.

So instead of the url you are currently using you have to use:

HtmlDocument htmlDoc = hw.Load(@"");

Then your code works. I'm still looking for the place this request gets put together...

But the query looks rather easy to guess. For example the page request the url So all you have to do is use your url and build a new one starting after the #.

10/7/2013 7:44:02 PM

Popular Answer

I'd recommend using CsQuery ( and then it's as simple as:

var doc = CQ.CreateFromUrl(@"");
var nodes = doc.Find("span.thumbnail-details-grey");
foreach(var node in nodes)

Related Questions


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