Chrome copy XPath returning null when using HtmlAgilityPack

c# html-agility-pack xpath

Question

I'm following the answer given here: https://stackoverflow.com/a/18066671

string Url = "http://www.owgr.com/ranking";
        HtmlWeb web = new HtmlWeb();
        HtmlDocument doc = web.Load(Url);
        string worldRanking = doc.DocumentNode.SelectNodes("//*[@id=\"ranking_table\"]/div[2]/table/tbody/tr[1]/td[1]")[0].InnerText;

But the Xpath is returning a null reference. I've tried several variations but nothing's working, I've never used XPath before, am I missing something?

1
2
5/23/2017 11:46:19 AM

Accepted Answer

There is no <tbody/> element in that table, remove the /tbody axis step and your query works totally fine. See "Why does my XPath query (scraping HTML tables) only work in Firebug, but not the application I'm developing?" for details.

Additional hint: XPath also supports single quotes for strings, so you don't need to escape the double quotes.

This XPath expression will return the element you're looking for:

//*[@id='ranking_table']/div[2]/table/tr[1]/td[1]
2
5/23/2017 11:46:19 AM


Related Questions





Related

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