sir_thursday
sir_thursday

Reputation: 5419

With HtmlAgilityPack, verify that element on webpage exists

Let's say I'm on http://google.com, and I want to verify that there is an element with id="hplogo" that exists on the page (which there is, it's the Google logo).

I want to use HtmlAgilityPack, so I write something like this:

    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    doc.LoadHtml("http://google.com");
    var foo = (from bar in doc.DocumentNode.DescendantNodes()
               where bar.GetAttributeValue("id", null) == "hplogo"
               select bar).FirstOrDefault();
    if (foo == null)
    {
        HasSucceeded = 1;
        MessageBox.Show("not there");
    }
    else
    {
        MessageBox.Show("it's there");
    }
    return HasSucceeded;
}

Which should return the "it's there" message because it is there. But it doesn't. What am I doing wrong?

Upvotes: 1

Views: 1639

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236328

Method LoadHtml(html) loads string, which contain html content for parsing. This is not url of resource to load. So you are loading string "http://google.com" and trying to find logo in it. Which of course gives you not there result.

You can use WebClient to download resource content:

WebClient client = new WebClient();
string html = client.DownloadString("http://google.com");
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);

Upvotes: 3

Related Questions