Vb.net webbrowser documentcompleted event – page not loading

vb.netwebbrowser-control

I have searched the other questions surrounding the WebBrowser DocumentCompleted event, but no-one seems to have the exact problem that I am having.

I am trying to automate searching flights on an airline website. The first url I use is the same every time except for the date part so it's easy enough to get the WebBrowser to go the URL by combining strings. However, that page is a disclaimer page that has a 'proceed' button that needs to be clicked before prices are shown. If I use a series of buttons on a form, I can get to the first URL by clicking button1, and then click the proceed button by clicking button2. It works fine.

However, I wanted to remove the need to click button2 so attempted to use the WebBrowser DocumentCompleted event. The problem that I am having is that the first page never seems to fully load in the webbrowser and so the button is never clicked.

This is the code that I am using for the two buttons and the DocumentCompleted event

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    TextBox1.Text = fullURL
    WebBrowser1.Navigate(fullURL)

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim allElements As HtmlElementCollection = WebBrowser1.Document.All

    For Each webpageelement As HtmlElement In allElements

        If webpageelement.GetAttribute("src") = proceedbuttonattribute Then

            webpageelement.InvokeMember("click")

        End If

    Next

    End Sub

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

    If TextBox1.Text.StartsWith(firstURL) = True Then 'make sure that button is only clicked after first webpage loads

        Dim allElements As HtmlElementCollection = WebBrowser1.Document.All

        'Click 'Proceed to Booking' button
        For Each webpageelement As HtmlElement In allElements

            If webpageelement.GetAttribute("src") = proceedbuttonattribute Then

                webpageelement.InvokeMember("click")

            End If

        Next

    End If

    End Sub

Thanks!

Best Answer

WebBrowser1.Navigate(fullURL)
    Do While wb.ReadyState <> WebBrowserReadyState.Complete
        Application.DoEvents()

    Loop

;)

Related Topic