我怎么能等到webbrowser加载页面?
我试过了:
webbrowser1.navigate(url)
msgbox("done")
答案 0 :(得分:7)
这是我遇到同样问题时使用的方法。通过添加处理程序,您不必使用计时器进行不必要的处理,而是在文档加载后立即触发事件。不要被名称documentcompleted所欺骗,它实际上正在等待加载网页。
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
我不是说这是最好的方式,但对我来说效果很好:)
答案 1 :(得分:0)
Do While wb.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop
答案 2 :(得分: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