HTML Agility Pack - Using XPath to Get a Single Node - Object Reference Not Installed in an Object Instance

This is my first attempt to get the value of an element using HAP. I get an error with a null object when I try to use InnerText.

URL that I am cleaning: -    http://www.mypivots.com/dailynotes/symbol/659/-1/e-mini-sp500-june-2013 I am trying to get the value for the current maximum from the pivot table of the day’s changes.

My code is below. First, I just wanted to know if I would do it right? If so, is my XPath value just wrong?

XPath value was obtained using a utility that I found called htmlagility helper. The XPb firebug version below also gives the same error: - / HTML / body / DIV [3] / DIV / table / TBODY / tr [3] / TD / table / TBODY / tr [5] / td [3]

My code: -

WebClient myPivotsWC = new WebClient();
string nodeValue;
string htmlCode = myPivotsWC.DownloadString("http://www.mypivots.com/dailynotes/symbol/659/-1/e-mini-sp500-june-2013");
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(htmlCode);
HtmlNode node = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[3]/div[1]/table[1]/tbody[1]/tr[3]/td[1]/table[1]/tbody[1]/tr[5]/td[3]");
nodeValue=(node.InnerText);

Thanks, Will.

+5
source share
2 answers

You cannot rely on development tools like FireBug or Chrome, etc. to determine the XPATH for your sites, since the XPATH provided by such tools corresponds to the HTML DOM, while the Html Agility Pack only knows about raw HTML sent by server.

, , ( ). , TBODY, . - XPATH. , XPATH, , , - "" , .

, :

HtmlNode node = doc.DocumentNode.SelectSingleNode("//td[@class='dnTableCell']//a[text()='High']/../../td[3]");

, :

  • TD CLASS, 'dnTableCell'.//token , XML.
  • A, ( ), "".
  • ( TR)
+22

Simon Mourier explaind, HTML, . , , , , DOM. - DOM, HTML . WatiN :

WatiN.Core.Settings.MakeNewInstanceVisible = false;
WatiN.Core.Settings.AutoMoveMousePointerToTopLeft = false; 
IE ie = new IE();
ie.GoTo(urlLink); 
ie.WaitForComplete();
string html = ie.Html;
ie.close();
+2

All Articles