我一直在使用计时器,只是观察特定元素内容的变化。
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 事件和 Timer 上的 Tick 事件。
我为每个所需的操作编写 DocumentComplete 或 Tick 处理程序,每个处理程序通常都会自己删除处理程序,因此成功的事件只会被处理一次。我还有一个名为 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.