Hello,

I am trying to make a program which will login to a website with many different accounts one after another.

The problem I have though is, the documentCompleted event either doesn't run at all or it runs more than once before the page is fully loaded. This causes the application to throw an error because the input fields for the login form are not loaded yet...

Here is the code I have in the document completed event:

 If (WebBrowser1.Url.ToString = urlInfo.Text) Then
                Dim userEl = WebBrowser1.Document.GetElementById(usernameForm.Text)
                userEl.SetAttribute("value", usernameCred.Text)
                Dim passEl = WebBrowser1.Document.GetElementById(passForm.Text)
                If (usePassFile And passCurLine < passFileLinesCount) Then
                    passEl.SetAttribute("value", passFileLines(passCurLine))
                    statusBox.Text = "Trying " & passFileLines(passCurLine) & "..."
                    passCurLine += 1
                    Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
                    For Each element As HtmlElement In allelements
                        If (element.GetAttribute("id") = loginForm.Text) Then
                            element.InvokeMember("click")
                        End If
                    Next
                End If
                MsgBox("Ran")
            Else
                statusBox.Text = "Successfully logged in"
            End If

Any help?
Thanks

give this pattern a try

   Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
      If WebBrowser1.Url = e.Url Then
         ' this is the one you want
         ' run your code against WebBrowser1.Document
      End If
   End Sub

It still doesn't work correctly. It enters the username and password and clicks login but after a couple of tries it starts going really fast and not waiting for the response to see if the login was correct or not. Any ideas?

It looks like your code is to automatically login to some site. Presumably you would only want to do this login once, so try adding a boolean check to prevent the login code from running on each Doc_complete event. Or instead of WebBrowser1.Url, use a Url variable set to the login page an test that against e.Url.

   Private LoggedIn as Boolean = False
   Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
      If WebBrowser1.Url = e.Url Then
         If Not LoggedIn then
            LoggedIn = True
            ' Run your login code
         End If
      End If
   End Sub

   ' Or 

      Private urlLogin as Uri = New Uri("www.loginaddress")
   Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
      If urlLogin = e.Url Then

            ' Run your login code

      End If
   End Sub

I'm trying to make it read each line from a text file and try to login to an account using that particular line - then if it doesn't log in then it should try the next line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.