Detecting completely loaded page using VB.Net WebBrowser including all frames

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.

Like it? Share it:

4 thoughts on “Detecting completely loaded page using VB.Net WebBrowser including all frames

  1. Keith

    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

    Reply
  2. Keith

    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

    Reply
  3. Keith

    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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of follow-up comments via e-mail. You can also subscribe without commenting.