2

我正在尝试获取元素的子元素的 InnerHtml。这是我所拥有的:

If doc.GetElementById("ctl00_cphBanner_MenuRedesign_BannerAlertsAndOptionsLoginView_BannerAlertsAndOptions_Authenticated_FriendsBubble") IsNot Nothing Then
                    Dim el As HtmlElement = doc.GetElementById("ctl00_cphBanner_MenuRedesign_BannerAlertsAndOptionsLoginView_BannerAlertsAndOptions_Authenticated_FriendsBubble")
                    inboxTxt.Text = el.Children(1).Children(0).InnerHtml.ToString
                End If

这是我收到的错误:

"Object reference not set to an instance of an object."

我该如何解决?

编辑:当我删除“尝试”功能时,错误显示在这里:

If doc.GetElementById("ctl00_cphBanner_MenuRedesign_BannerAlertsAndOptionsLoginView_BannerAlertsAndOptions_Authenticated_FriendsBubble") IsNot Nothing Then
4

2 回答 2

1

您正在假设您的doc对象具有价值。在检查子元素之前,尝试检查它是否也没有。

If Not IsNothing(doc) Then
    If Not IsNothing(doc.GetElementById("ctl00_cphBanner_MenuRedesign_BannerAlertsAndOptionsLoginView_BannerAlertsAndOptions_Authenticated_FriendsBubble")) Then
        Dim el As HtmlElement = doc.GetElementById("ctl00_cphBanner_MenuRedesign_BannerAlertsAndOptionsLoginView_BannerAlertsAndOptions_Authenticated_FriendsBubble")
        inboxTxt.Text = el.Children(1).Children(0).InnerHtml.ToString
    End If
End If

更新代码。这有效,但不返回您的HtmlElement

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        wb.Navigate("http://www.roblox.com/user.aspx?id=3659905")
    End Sub

    Private Sub Form1_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        Dim doc As HtmlDocument = wb.Document
        If Not IsNothing(doc) Then
            Dim el As HtmlElement = doc.GetElementById("ctl00_cphBanner_MenuRedesign_BannerAlertsAndOptionsLoginView_BannerAlertsAndOptions_Authenticated_FriendsBubble")
            If el IsNot Nothing Then
                inboxTxt.Text = el.Children(1).Children(0).InnerHtml.ToString
            Else
                inboxTxt.Text = "No Data"
            End If
        End If
    End Sub
End Class
于 2012-11-18T04:47:07.580 回答
0

最有可能的是,至少有一个表达式el.Children(1)el.Children(1).Children(0)el.Children(1).Children(0).InnerHtml导致 null/Nothing。按顺序检查每一个,以确保您确实具有价值。

于 2012-11-18T04:42:51.333 回答