FizzlerEx/HtmlAgilityPack QuerySelectorAll not working

c# html-agility-pack

Question

I've been attempting to set up FizzlerEx, found at http://fizzlerex.codeplex.com/. After adding the references to my project, I've attempted to run the example code given on the website- the entirety of my code is listed below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using Fizzler.Systems.HtmlAgilityPack;


namespace Fizzler_Test
{
    class Program
    {

        static void Main(string[] args)
        {
        var web = new HtmlWeb();
            var document = web.Load("http://example.com/page.html");
            var page = document.DocumentNode;

            foreach (var item in page.QuerySelectorAll("div.item"))
            {
                var title = item.QuerySelector("h3:not(.share)").InnerText;
                var date = DateTime.Parse(item.QuerySelector("span:eq(2)").InnerText);
                var description = item.QuerySelector("span:has(b)").InnerHtml;
            }
        }
}

However, this yields build errors, claiming that:

Error   1   'HtmlAgilityPack.HtmlNode' does not contain a definition for 'QuerySelectorAll' and no extension method 'QuerySelectorAll' accepting a first argument of type 'HtmlAgilityPack.HtmlNode' could be found (are you missing a using directive or an assembly reference?)

It would seem that QuerySelectorAll is not actually a part of HtmlNode, but given that this is the official example code taken verbatim from the website, I'd expect the creators understand how their library works. I'm at a loss as to what the actual issue could be.

A related problem seems to have been found here, but no suitable answer was ever found: Fizzler and QuerySelectorAll

Popular Answer

It would seem that QuerySelectorAll is not actually a part of HtmlNode, but given that this is the official example code taken verbatim from the website, I'd expect the creators understand how their library works. I'm at a loss as to what the actual issue could be.

You are correct about this part. Though you are not correct about the second part, since the author of HAP isn't the author of FizzlerEx. The problem is elsewhere. Simply by looking at the error, you get the only clue you need to go to solve this .

Error   1   'HtmlAgilityPack.HtmlNode' does not contain a definition for 'QuerySelectorAll' and no extension method 'QuerySelectorAll' accepting a first argument of type 'HtmlAgilityPack.HtmlNode' could be found (are you missing a using directive or an assembly reference?)

So, what do we get; it tells us that there is no method called QuerySelectorAll in the class HtmlNode in the namespace HtmlAgilityPack. If we take a look at the source code of HAP, you can easily determine that the error message is indeed correct, since there is no method by that name in the class we are looking.

Source code for HtmlAgilityPack.HtmlNode - class

Where is this method that we want to use, but cannot find?

It's here, In the Fizzler.Systems.HtmlAgilityPack.HtmlNodeSelection-class.

After trying a few things, I get the code to work perfectly just as it is. The problem was the extensions in the reference between Fizzler and HAP source code.

If you download Fizzler you get HtmlAgilityPack at the same time. When you add the references in Visual Studio (assuming you use that), only add

  • Fizzler.Systems.HtmlAgilityPack.dll
  • HtmlAgilityPack.dll

Clean your solution and rebuild it and it should work!



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why