1

我怎样才能等到 webbrowser 加载页面?
我试过了:

webbrowser1.navigate(url)
msgbox("done")
4

3 回答 3

8

这是我遇到同样问题时使用的方法。通过添加处理程序,您不必使用计时器进行不必要的处理,而是在文档加载后立即触发事件。不要被名称文件完成所迷惑,它实际上是在等待网页加载。

AddHandler (webbrowser1.DocumentCompleted), AddressOf WebpageLoaded
webbrowser1.Navigate(url)

Public Sub WebpageLoaded(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
    MessageBox.Show("Done")
End Sub

我不是说这是最好的方法,但对我来说效果很好:)

于 2012-06-19T11:39:29.760 回答
1
Do While wb.ReadyState <> WebBrowserReadyState.Complete
    Application.DoEvents()
Loop
于 2012-06-19T11:17:23.687 回答
0

我从 WebBrowser 控件继承了一个新类:

   Public Class WebBrowserSyncFW
        Inherits WebBrowser

        Public Async Function NavigateSync(ByVal urlString As String, Optional ByVal timeoutmillisec As Integer = 30000) As Task(Of Boolean)
            Dim IsLoaded As Boolean = False

            Me.ScriptErrorsSuppressed = True
            Me.Navigate(urlString)

            AddHandler Me.DocumentCompleted, Sub(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
                                                 IsLoaded = True
                                             End Sub

            For i = 1 To timeoutmillisec / 100
                Await Task.Delay(100).ConfigureAwait(False)

                If IsLoaded = True Then Return True
            Next

            Return False
        End Function

    End Class

用法:

        If Await WebBrowserSyncFW1.NavigateSync("http://www.youtube.com") Then
            MsgBox("Page is loaded!", MsgBoxStyle.Information)
        Else
            MsgBox("Timeout!", MsgBoxStyle.Exclamation)
        End If
于 2015-08-27T12:46:34.617 回答