html - 对象引用未设置为对象的实例 - vb.net

标签 html vb.net webbrowser-control

我正在尝试获取元素的子元素的子元素的 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/

相关文章:

.net - 是否可以将 Selenium 与包含 WebBrowser 对象的 C# windows 窗体一起使用?

css - 分割父 div 高度以用于子 div

html - 自定义字体未显示在我的网络预览中

javascript - jQuery 的简单子(monad)菜单故障

asp.net - 限制用户的访问

c# - WebBrowser 文本选择更改时触发事件

html - CSS页脚列间距

mysql - 更新 MySQL 数据库时出错 : DUPLICATE DEFAULT ENTRY FOR PRIMARY KEY = '0'

asp.net - 如何访问 ListView 中的按钮?特别是使用button_click来执行操作

c# - WPF WebBrowser 字体大小更改功能