GetElementById return incorrect nodes

c# html-agility-pack

Question

I'm encountered a little problem (I don't know if I did some mistake or if is a bug) while parsing this page.

I'm trying to get all the a tag available in this table:

enter image description here

so for achieve this, I wrote this code:

`var d = doc.GetElementbyId("odds-data-table");
HtmlNodeCollection listItems = d.SelectNodes("//a");`

in particular d contains the table structure that I want:

enter image description here

but the listItems variable doesn't contains the link of the table but of the whole html page, and this is pretty weird. I tried different case:

d.SelectNodes("a") : return null
d.SelectNodes("//a") : return all the link of the page 
d.SelectNodes("/a") : return null

what is wrong? And I would ask also which type of plugin or system did you use for HtmlAgilityPack documentation, is really stunning, thanks.

1
0
8/8/2018 10:44:05 AM

Accepted Answer

You have to read Attributes property for each of your HtmlNode from your HtmlNodeCollection like

HtmlDocument doc = new HtmlDocument();
var d = doc.GetElementbyId("odds-data-table");
HtmlNodeCollection listItems = d.SelectNodes(".//a");

//This list contains all your href values
List<string> hrefs = new List<string>();
foreach (var item in listItems)
{
      var href = item.Attributes["href"].Value;
      hrefs.Add(href);
}
0
8/8/2018 1:27:38 PM


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