Something I depend on quite frequently is determining when a web page in a WebBrowser control has completely loaded, including all frames. If you’ve ever worked with this control and relied on the DocumentCompleted event, you may have come across the inconsistency with it when dealing with pages that contain HTML frames. The little snippet below does a very good job with detecting completely loaded pages using the VB.Net WebBrowser control, including all HTML frames on the page. Works with AJAX too!
Private Sub WebBrowser1_DocumentCompleted( _
ByVal sender As Object, _
ByVal e As WebBrowserDocumentCompletedEventArgs _
) Handles WebBrowser1.DocumentCompleted
If Me.WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
MsgBox("completely loaded")
End If
End Sub
Comments welcome.
Nice little trick
I’m sure that will come in handy
Thanks for that, works like a treat.
I’ve used it in the main code when automating several pages, so I used a Boolean before executing the sub.
I set the Boolean e.g. iReady to false, then run the sub which navigates a page, then set the
iREADY = False
While iREADY = False
status.Text = “Busy.. waiting for page load!”
status.Refresh()
End While
And the WebBrowser1 completed setting the Boolean ready back to true
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If Me.WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
‘status.Text = “Web page loaded!”
iREADY = True
End If
‘ImBUSY = False
End Sub
sorry missed out executing the automated webpage sub eg:
iREADY = False
SelectNEXT() ‘ sub to click next on screen
While iREADY = False
status.Text = “Busy.. waiting for page load!”
status.Refresh()
End While
‘ page loaded continue to next page clicks
Sorry, one more addition, I don’t usually post comments but I struggled with this for ages and now I’ve fixed it I thought I’d share it.
Unfortunately I can’t delete my post and re-edit so one more addition I’m afraid…
You must put
Application.DoEvents()
In the loop or it will hang and nothing will happen. E.g.:
While iREADY = False
status.Text = “Busy.. waiting for page load!”
status.Refresh()
Application.DoEvents()
End While