"Object reference not set to an instance of an object" and HtmlAgility

384 Views Asked by At

i have a problem that confused me ! i want to scraping a value from a webpage. i found xpath with chrome and inspector "//*[@id='GlobalTab0Elm']/div[2]/div[1]/div[2]/table/tbody/tr[7]/td[2]/div"

when i use above xpath in webscraper in chrome (extension) it works fine. but the problem is this isnt work on my simple program that you can see this :

     Dim Handler As HtmlAgilityPack.HtmlWeb.PreRequestHandler = Function(request As HttpWebRequest)
                                                                       request.Headers(HttpRequestHeader.AcceptEncoding) = "gzip, deflate"
                                                                       request.AutomaticDecompression = DecompressionMethods.Deflate Or DecompressionMethods.GZip
                                                                       request.CookieContainer = New System.Net.CookieContainer()
                                                                       Return True
                                                                   End Function
        Dim webClient As HtmlWeb = New HtmlWeb()
        webClient.PreRequest = Handler


        webClient.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5"
        webClient.UseCookies = True

        Dim htmlDoc = webClient.Load("http://www.tsetmc.com/Loader.aspx?ParTree=15")
        htmlDoc.OptionReadEncoding = False
        Dim S As String
        S = "//*[@id='GlobalTab0Elm']/div[2]/div[1]/div[2]/table/tbody/tr[7]/td[2]/div"
        Dim node = htmlDoc.DocumentNode.SelectSingleNode(S)
    TextBox1.Text = (node.InnerText)

my qustion is why this xpath works on the other scraper but it didnt work on these code? what i have to do ? The err happen at this line :

TextBox1.Text = (node.InnerText)

and the error text is

Object reference not set to an instance of an object.

when i use this xpath

"//*[@id='company_text']/text()[2]"

it found the correct value of xpath.

my problem is it doesnt work with this xpath.

"//*[@id='GlobalTab0Elm']/div[2]/div[1]/div[2]/table/tbody/tr[7]/td[2]/div"
1

There are 1 best solutions below

1
On

I found the solution

 For Each li As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//tbody")


            ArzeshNode = li.SelectSingleNode("//*[contains(@class,'table1')]/tbody/tr[7]/td[2]/div")   

        Next
msgbox( SplitValue((ArzeshNode.InnerText)) )