I’m having a problem using some of the XPaths given to me by Chrome.
The example I’m trying to work with right now is at http://magic.tcgplayer.com/db/magic_single_card.asp?cn=Jace,%20Memory%20Adept
I’m trying to isolate the price with the blue background at the top of the page.
Chrome says that the xpath is
//*[="inputForm"]/table[1]/tbody/tr[1]/td/div/table/tbody/tr/td[5]/center/b
and based on that I’m trying to create a string with the price amount by doing this:
var baseNode = document.DocumentNode.SelectSingleNode("//*[=\"inputForm\"]//table[1]//tbody//tr[1]//td//div//table//tbody//tr//td[5]//center//b");
String price = baseNode.InnerText;
Which crashes with the message
Object reference not set to an instance of an object.
I’m not sure what to do here and I could use some help.
Thanks!
Most of the time, you cannot take an XPATH given by a browser debug tool (Chrome, FF, etc…) and use it as is in a tool such as Html Agility Pack.
The reason is a browser gives you the XPATH of an in-memory element, while Html Agility Pack will see the XPATH from the source HTML stream.
The most frequent cases of discrepancy between the two XPATHs expressions is element that are added by browsers; for example TBODY. TBODY is rarely defined in HTML files, but is always added by browsers. So you could try this:
but I certainly don’t guarantee it to work. The best is to have an human look at the source HTML and come up with a good discriminant (and future tolerant) XPATH expression, something like this for example:
Meaning “get all ‘myElement’ tags which have a ‘class’ attribute with ‘someclass’ as the value.
We could help more with the HTML source and what element you want to get at.
EDIT: to get the price displayed in blue in the url you gave, if you take a quick look at it, you will see the discriminant is precisely the color, so you can imagine an XPATH like this:
Wich means:
So a C# code like this:
will display