DotNetRussell
DotNetRussell

Reputation: 9857

Can't retrieve input tags with HTML Agility Pack

I am attempting to scrape a random site for input tags.

So I need to write this in such a way that it will work with most sites.

Currently I have

void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{

    HtmlAgilityPack.HtmlDocument dom = new HtmlAgilityPack.HtmlDocument();

    dom.LoadHtml(e.Result);

    var node = dom.DocumentNode.Element("html");

    var inputs = node.ChildNodes["body"].Descendants("input");

but this isn't working.

node returns with the data I want but inputs always turns out null.

When I do node.ChildNodes["body"].Descendants().ToList(); I see entries named "input".

enter image description here

Am I missing something?

Upvotes: 0

Views: 244

Answers (1)

har07
har07

Reputation: 89305

Your code is fine, it's just the way you verified was wrong. All those null values in the screenshot doesn't indicate that the IEnumerable<T> is empty, you can try to call inputs.ToArray() or inputs.ToList() in watch window instead to verify the result.

Remember that IEnumerable<T> lazy-load the items until necessary, for example, until you iterate through, call ToList()/ToArray(), etc.

Upvotes: 1

Related Questions