XPath in VB.NET with HTML Agility pack

html-agility-pack vb.net xpath


i have the following vb.net code which works fine and in the message box i see the exact number of elements which have an id attribute.

Dim hreftext = htmldoc.DocumentNode.SelectNodes("//*[@id]")

now the problem is when i use the following on the message box it gives me Object reference not set to an instance of an object although there are 6 elemnts with the id rso

Dim hreftext = htmldoc.DocumentNode.SelectNodes("//*[@id='rso']")

is there any thing wrong with the second snippet?

11/2/2012 3:24:20 AM

Accepted Answer

Having read another one of your SO questions, it seems you are trying to scrape google shopping, and have neglected to check the downloaded html source, rather than the generated dom.

The id "rso" does not appear in the html source, which is why you are getting the error. Google is not keen on being scraped, and certainly make it difficult.

To see what i mean, you can add a multiline textbox to your form and add this instead of your current xpath code:

TextBox1.Text = htmldoc.DocumentNode.OuterHtml

Pretty isnt it!!

11/5/2012 12:51:10 PM

Popular Answer

To elaborate on checking the case:


Dim hreftext = htmldoc.DocumentNode.SelectNodes("//*[translate(@id,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='rso']")

To grab any node that is equal to any case combination of 'rso'

Related Questions


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