我一直在使用一個計時器,只是看在特定元素含量的變化。
Private AJAXTimer As New Timer
Private Sub WaitHandler1(ByVal sender As Object, ByVal e As System.EventArgs)
'Confirm that your AJAX operation has completed.
Dim ProgressBar = Browser1.Document.All("progressBar")
If ProgressBar Is Nothing Then Exit Sub
If ProgressBar.Style.ToLower.Contains("display: none") Then
'Stop listening for ticks
AJAXTimer.Stop()
'Clear the handler for the tick event so you can reuse the timer.
RemoveHandler AJAXTimer.Tick, AddressOf CoveragesWait
'Do what you need to do to the page here...
'If you will wait for another AJAX event, then set a
'new handler for your Timer. If you are navigating the
'page, add a handler to WebBrowser.DocumentComplete
End If
Exit Sub
Private Function InvokeMember(ByVal FieldName As String, ByVal methodName As String) As Boolean
Dim Field = Browser1.Document.GetElementById(FieldName)
If Field Is Nothing Then Return False
Field.InvokeMember(methodName)
Return True
End Function
我有2個對象獲得事件處理程序,WebBrowser和Timer。 我主要依賴WebBrowser上的DocumentComplete事件和定時器上的Tick事件。
我要求每個操作都寫DocumentComplete或Tick處理程序,每個處理程序通常都是RemoveHandler本身,所以一個成功的事件只能處理一次。我還有一個名爲RemoveHandlers的過程,它將從瀏覽器和計時器中刪除所有處理程序。
我的AJAX命令通常是這樣的:
AddHandler AJAXTimer.Tick, AddressOf WaitHandler1
InvokeMember("ContinueButton", "click")
AJAXTimer.Start
我的導航命令,如:
AddHandler Browser1.DocumentComplete, AddressOf AddSocialDocComp
Browser1.Navigate(NextURL) 'or InvokeMember("ControlName", "click") if working on a form.