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)またはの少なくとも1つがel.Children(1).Children(0).InnerHtmlnull/Nothingになります。それらのそれぞれを順番にチェックして、実際に値があることを確認してください。

于 2012-11-18T04:42:51.333 に答える