0

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
Chris
  • 8,527
  • 10
  • 34
  • 51
  • Check [this](http://stackoverflow.com/a/18734602/1768303) for some thoughts on the `Application.DoEvents()` loop. – noseratio Sep 13 '13 at 08:22

1 Answers1

1

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

Don Thomas Boyle
  • 3,055
  • 3
  • 32
  • 54
  • I appreciate it. I'm trying to test but I get pageready is not declared error. I found the code posted in another answer, it also has Private Property pageready() As Boolean but when I add that, I get more errors. – user2773260 Sep 12 '13 at 17:35
  • Please update your question and show me where you are putting it. You should be able to copy it just how i have it, remove the plain text and change the button name to your buttonName and run the code. – Don Thomas Boyle Sep 12 '13 at 17:50
  • This is all the code that you have provided, stacked up and how its supposed to look. this should work, granted you have webbrowswer1 and button1. If you receive errors write back with the error that you got. – Don Thomas Boyle Sep 12 '13 at 18:16
  • I really appreaciate you taking your time and helping me. I copied/pasted the final code, here the error list; 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 – user2773260 Sep 12 '13 at 18:26
  • Try that again, no errors posted. Retryed this logic in my own Application jsut how it is posted in the update and it runs and logs me in. – Don Thomas Boyle Sep 12 '13 at 18:27
  • Another question your tags say vb6 and vb.net , wich are you coding in. the code i provided is vb.net – Don Thomas Boyle Sep 12 '13 at 18:28
  • can you send the new errors out according to the new updates in my answer? – Don Thomas Boyle Sep 12 '13 at 18:56
  • End of statement expected Line 3 - Column 39 /// Statement cannot appear within a property body. End of property assumed. Line 5 - Column 1 /// Name 'pageready' is not declared. Lines 28-31-36 Columns 19-9-13 – user2773260 Sep 12 '13 at 19:00
  • Make sure you are closing all your `Sub` with `End Sub`, `Function` with `End Function`, `Class` with `End Class` please, according to http://forums.asp.net/t/1530259.aspx this is what is causing your error. – Don Thomas Boyle Sep 12 '13 at 19:05
  • My assumption is that you are taking my code and not fully replacing yours. Rather you are pasting it inside of something causing the error – Don Thomas Boyle Sep 12 '13 at 19:08
  • I started a new project from scratch, but still the same error. Should I try a different application than visual studio? – user2773260 Sep 12 '13 at 19:11
  • No, while i edit the code above please start new aplication, with 1 webbrowswer, and 1 button, 2 textboxes. – Don Thomas Boyle Sep 12 '13 at 19:17
  • I uploaded you a picture with exactly what you should see (minus) the Form2 - should be Form1 and the squiggly lines should be good objects.properties. Hope this helps , unsure what else to do here for you. – Don Thomas Boyle Sep 12 '13 at 19:22
  • It is exactly the same but somehow same errors in visual studio. I just tried in visual basic 2010, codes had no error but the function didn't do what it suppose to. I'll play with it to see if I can make it work. – user2773260 Sep 12 '13 at 19:26