0
votes

I have the follow xpath '/html/body/div[4]/div[2]/div/div/h1' for 'http://stackoverflow.com/', that is 'Top Questions' header.

How to use it in HtmlAgilityPack?

var wc = new WebClient();
wc.Encoding = Encoding.UTF8;
var html = wc.DownloadString("http://stackoverflow.com/");
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var node = htmlDoc.DocumentNode.SelectNodes("/html/body/div[4]/div[2]/div/div/h1");

returns null

1
Can you check downloaded page? Does it have expected structure? - Kirill Polishchuk
Please, try this one: //body/div[4]/div[2]/div/div/h1. - Kirill Polishchuk
Yep. And I have same problem with all xpaths on any htmls. So I want to know, may be there is some peculiar properties with xpath in agility pack? Sometimes it throws 'Expression must evaluate to a node-set.' excpetion - Evgraf
Probably some setup issue on your computer. I copied your code directly and received nodes. Check to make sure your html var has data? - Bert

1 Answers

1
votes

Works for me. The .Dump() is just from using it in LinqPad.

var url = "http://stackoverflow.com/";
new HtmlWeb().Load(url)
             .DocumentNode
             .SelectSingleNode("/html/body/div[4]/div[2]/div/div/h1")
             .InnerText.Dump();

Result:

    Top Questions    

With some spacing.