我正在尝试获取元素的子元素的子元素的 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."
我该如何解决这个问题?
编辑:当我删除“Try”函数时,错误显示在这里:
If doc.GetElementById("ctl00_cphBanner_MenuRedesign_BannerAlertsAndOptionsLoginView_BannerAlertsAndOptions_Authenticated_FriendsBubble") IsNot Nothing Then
最佳答案
您假设您的 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
关于html - 对象引用未设置为对象的实例 - vb.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13437551/