WebBrowser在后续使用中失败吗? WebBrowser1_DocumentCompleted不起作用

时间:2019-02-18 08:13:12

标签: vb.net winforms

使用“ WebBrowser”和HTMLDocument,HTMLTable,HTMLTableRow检索HTML表格行,列的innerText的VB.Net窗口表单。它仅在第一次使用时有效,但在随后的时间内失败。

    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,但仍然无法使用。 任何线索都有帮助。谢谢。

2 个答案:

答案 0 :(得分:1)

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

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

WebBrowser1.ReadyState <> WebBrowserReadyState.Complete

为避免出现后期绑定警告或错误,请将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

答案 1 :(得分:0)

最后,我认为, jmcilhinney 中的确保所有转换和转换均已明确完成

rows.cells(0).innerText    ===> Will fail on the subsequent use but do not know why the first time is OK

rows = table.rows.item(r)  ====> OK, if all casts and conversions are done explicitly
cell0 = rows.cells.item(0)

谢谢...