user2773260
user2773260

Reputation: 1

Web Browser Events

I get following errors on the codes below;

End of statement expected Line 3 - Column 39 /// Error 2 Statement cannot appear within a property body. End of property assumed. Line 5 - Column 1 /// Error 3 Name 'pageready' is not declared. Lines 28-31-36 Columns 19-9-13

Public Class Form1

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

        'Button1 Code
        WebBrowser1.Document.GetElementById("Email").SetAttribute("value", TextBox1.Text)
        WebBrowser1.Document.GetElementById("Passwd").SetAttribute("value", TextBox2.Text)
        WebBrowser1.Document.GetElementById("signIn").InvokeMember("click")
        WaitForPageLoad()

        'Button2 Code
        For Each acct As HtmlElement In WebBrowser1.Document.GetElementsByTagName("a")
            If acct.GetAttribute("href").Contains("https://accounts.google.com/b/0/PlusPageSignUp") Then
                acct.InvokeMember("click")
                WaitForPageLoad()
            End If
        Next

    End Sub

Private Property pageready As Boolean = False
    End Property

#Region "Page Loading Functions"

    Private Sub WaitForPageLoad()

        AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
        While Not pageready
            Application.DoEvents()
        End While
        pageready = False
    End Sub

    Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
        If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
            pageready = True
            RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
        End If
    End Sub

#End Region


End Class

Upvotes: 0

Views: 1414

Answers (1)

Don Thomas Boyle
Don Thomas Boyle

Reputation: 3045

Basically every time the Browswer is busy you have to wait on it to return...

Combine all your buttons ( seperated by a Wait on the browswer ) ..

Combine them into something like this

Private Sub WhateverButtonName(sender As Object, e As EventArgs) Handles WhateverButtonName.Click

    'Button1 Code
    WebBrowser1.Document.GetElementById("Email").SetAttribute("value", TextBox1.Text)
    WebBrowser1.Document.GetElementById("Passwd").SetAttribute("value", TextBox2.Text)
    WebBrowser1.Document.GetElementById("signIn").InvokeMember("click")
    WaitForPageLoad()

    'Button2 Code
     For Each acct As HtmlElement In WebBrowser1.Document.GetElementsByTagName("a")
     If acct.GetAttribute("href").Contains("https://accounts.google.com/b/0/PlusPageSignUp") Then
          acct.InvokeMember("click")
          WaitForPageLoad()
     End If
     Next



End Sub

Additionally Use this code to run the Wait Subs ( Sorry no source i forget where i found it)

#Region "Page Loading Functions"

Private Sub WaitForPageLoad()
    AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
    End While
    pageready = False
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub

#End Region

EDIT: Added Full Code

Public Class Form1

    Private Property pageready As Boolean = False

Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    'Button1 Code
    WebBrowser1.Navigate("https://accounts.google.com/Login")
    WaitForPageLoad()
    WebBrowser1.Document.GetElementById("Email").SetAttribute("value", TextBox1.Text)
    WebBrowser1.Document.GetElementById("Passwd").SetAttribute("value", TextBox2.Text)
    WebBrowser1.Document.GetElementById("signIn").InvokeMember("click")
    WaitForPageLoad()

    'Button2 Code

End Sub

#Region "Page Loading Functions"

Private Sub WaitForPageLoad()

    AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
    End While
    pageready = False
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub

#End Region


End Class

Update

This is exactly what you should see, - the squiggly lines ( i replace my textboxes with actual values to test ). And I redid mine in Form2. You can Rename Form2 to Form1 for you.

enter image description here

Upvotes: 1

Related Questions