Vb.net WebBrowser在后续使用中失败?WebBrowser1\u文档已完成但不工作
一个VB.Net windows窗体,使用WebBrowser和HTMLDocument、HTMLTable、HTMLTableRow检索HTML表格行、列的内部文本。它只在第一次工作,但在随后的时间失败Vb.net WebBrowser在后续使用中失败?WebBrowser1\u文档已完成但不工作,vb.net,winforms,Vb.net,Winforms,一个VB.Net windows窗体,使用WebBrowser和HTMLDocument、HTMLTable、HTMLTableRow检索HTML表格行、列的内部文本。它只在第一次工作,但在随后的时间失败 Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
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.cells0.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,但仍然不起作用。
任何线索都有帮助。谢谢。使用界面和对象执行相同任务的两个示例 在处理事件时,我们首先检查它的属性。如果不是,则当前文档仍然没有准备好进行解析。请注意,每个HTML页面框架可以有多个HtmlDocument,iFrame可以有其个人文档,因此此事件可以在每页引发多次
WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
正如您在两个代码段中所看到的,在使用任何一个对象时,差异几乎不存在(在本例中):
使用mshtml.HTMLDocument:
最后,在我看来,确保所有的强制转换和转换都是显式完成的。吉姆西林尼说的没错
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)
感谢…错误消息与后期绑定有关,因此首先要打开Option Strict,并确保所有强制转换和转换都显式完成。如果仍然存在错误,您将获得更明确的信息。顺便说一句,您并不需要mshtml.HTMLDocument来完成此操作。您可以直接使用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 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
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
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)