首页 > 解决方案 > WebBrowser 在后续使用中失败?WebBrowser1_DocumentCompleted 不起作用

问题描述

VB.Net 窗体使用“WebBrowser”和 HTMLDocument、HTMLTable、HTMLTableRow 来检索 HTML Table 行、列的 innerText。它仅在第一次有效,但在后续失败。

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    Dim stockNo As String = ""
    Dim stockName String

    Dim doc As mshtml.HTMLDocument
    Dim table As mshtml.HTMLTable
    Dim rows As mshtml.HTMLTableRow

    doc = WebBrowser1.Document.DomDocument
    table = doc.getElementsByTagName("TABLE").item(0)
    For r = 3 To table.rows.length - 1
        rows = table.rows.item(r)

        Try
            stockNo = Replace(rows.cells(0).innerText, " ", "")
            stockName = Replace(rows.cells(1).innerText, " ", "")

        Catch ex As Exception
            Console.WriteLine("Error here: =====> " & ex.ToString)
            Console.WriteLine(rows.cells(0))              
        End Try
    Next r
End Sub    

这是在“rows.cells(0).innerText”上执行时的错误

Error here: =====> System.NotSupportedException: 發生例外狀況於 HRESULT: 0x800A01B6 
Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack)    
Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)    

也尝试 WebBrowser1_ProgressChanged 但仍然无法正常工作。任何线索都有帮助。谢谢。

标签: vb.netwinforms

解决方案


使用mshtml.HTMLDocument接口和WebBrowser Document对象 执行相同任务的两个示例。

在处理DocumentCompleted事件时,我们首先检查它的ReadyState。如果不是WebBrowserReadyState.Complete,则当前 Document 仍未准备好进行解析。HtmlDocument请注意,每个页面可以有多个HTML(Frames 和 IFrames 有它们的个人文档),因此每个页面可以多次引发此事件。

WebBrowser1.ReadyState <> WebBrowserReadyState.Complete

为避免 Late Bound 警告或错误,请将 WebBrowser 强制HtmlDocument转换为相同类型的局部变量。如果您使用该mshtml.HTMLDocument界面,则相同:

Dim wbDoc As HtmlDocument = DirectCast(sender, WebBrowser).Document
Dim htmlDoc As mshtml.HTMLDocument = DirectCast(wbDoc.DomDocument, mshtml.HTMLDocument)

正如您在两个代码片段中看到的那样,在使用任一对象时,差异是 - 在这种情况下 - 几乎不存在:

使用mshtml.HTMLDocument

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

    If WebBrowser1.ReadyState <> WebBrowserReadyState.Complete Then Return
    Dim startingRow As Integer = 3

    Dim wbDoc As HtmlDocument = DirectCast(sender, WebBrowser).Document
    Dim htmlDoc As mshtml.HTMLDocument = DirectCast(wbDoc.DomDocument, mshtml.HTMLDocument)

    Dim firstTable As mshtml.HTMLTable = htmlDoc.getElementsByTagName("TABLE").OfType(Of mshtml.HTMLTable)().FirstOrDefault()

    If firstTable IsNot Nothing Then
        For tableRow As Integer = startingRow To firstTable.rows.length - 1
            Dim row As mshtml.HTMLTableRow = DirectCast(firstTable.rows.item(tableRow), mshtml.HTMLTableRow)
            For col As Integer = 0 To 1
                Dim rowCell = DirectCast(row.cells.item(col), mshtml.HTMLTableCell)
                If rowCell IsNot Nothing Then
                    rowCell.innerText = rowCell.innerText?.Replace(" ", "")
                Else
                    'Decide what to do if the cell content is null
                End If
            Next
        Next
    End If
End Sub

直接使用WebBrowser.Document

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

    If WebBrowser1.ReadyState <> WebBrowserReadyState.Complete Then Return
    Dim startingRow As Integer = 3

    Dim doc As HtmlDocument = DirectCast(sender, WebBrowser).Document
    Dim firstTable As HtmlElement = doc.GetElementsByTagName("TABLE").OfType(Of HtmlElement)().FirstOrDefault()

    If firstTable?.Children.Count > 0 Then
        For tableRow As Integer = startingRow To firstTable.Children.Count - 1
            Dim rowCells As HtmlElementCollection = firstTable.Children(tableRow).Children

            If rowCells Is Nothing Then Continue For
            For col As Integer = 0 To 1
                If Not String.IsNullOrEmpty(rowCells(col).InnerText) Then
                    rowCells(col).InnerText = rowCells(col).InnerText.Replace(" ", "")
                Else
                    'Decide what to do if the cell content is null
                End If
            Next
        Next
    End If
End Sub

推荐阅读