0

Alright I have a script that logs into live.com, through this:

     WebBrowser1.Document.GetElementById("login").SetAttribute("value", txtUsername.Text)
     WebBrowser1.Document.GetElementById("passwd").SetAttribute("value", txtPassword.Text)
     WebBrowser1.Document.GetElementById("SI").InvokeMember("Click")

It's pretty basic, but now what I need is to find out if the user has typed a correct or wrong account credentials. So this would require an error message saying "Could not log in", but if it is successful I'll just redirect the webbrowser to a new page. Now i'm not familiar with IF statements on VB, but I do know that there is 2 ways this can be done, but don't know how to do it. So the 1st way is to read the URL it goes to after hitting the submit button, this would be pretty nice and work (so when someone type's incorrect account credentials it would send them to a error page, but when you type in the right one it would send you to the right page). But the main way I want to do it is through reading the content. If the page reads "That Microsoft account doesn't exist. Enter a different email address or get" after submitting, then message box "Wrong Account Credentials", then vice versa. I am not good with this because I have never really worked with the WebBrowser, but if anyone can lead me into the right way i'd be very thankful.

Sachin
  • 40,216
  • 7
  • 90
  • 102
Jack C.
  • 43
  • 3
  • 11

1 Answers1

0

If you want to evaluate by what is returned in the webbrowser, you can search the innerhtml of the document that loads as a result of the entered password:

 With WebBrowser1

        Do Until Not (.IsBusy)
            Application.DoEvents()
        Loop

        Do Until .ReadyState = WebBrowserReadyState.Complete
            Application.DoEvents()
        Loop


    End With

    Dim htmlText As String

    If Not (WebBrowser1.Document.Body.InnerHtml) Is Nothing Then

        htmlText = WebBrowser1.Document.Body.InnerHtml

        If InStr(htmlText, "That Microsoft account doesn't exist.") Then

            Messagebox.Show("Wrong Account Credentials")
            'code to go here if it is true

        Else

            Messagebox.Show("Correct Account Credentials")
            'code to go here if it is false


        End If

    End If

Note: if innerhtml doesn't work you can try outerhtml as well.

Since you only want to verify once, try removing the .DocumentChanged eventhandler as this is firing for different reasons other than the initial login; replace it as a subroutine called after you invoke the "Click" event on the login.

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
    WebBrowser1.Document.GetElementById("login").SetAttribute("value", txtUsername.Text)
    WebBrowser1.Document.GetElementById("passwd").SetAttribute("value", txtPassword.Text)
    WebBrowser1.Document.GetElementById("SI").InvokeMember("Click")

    verifyLogin()

End Sub


Private Sub verifyLogin()


    With WebBrowser1

        Do Until Not (.IsBusy)
            Application.DoEvents()
        Loop

        Do Until .ReadyState = WebBrowserReadyState.Complete
            Application.DoEvents()
        Loop


    End With

    Dim htmlText As String

    If Not (WebBrowser1.Document.Body.InnerHtml) Is Nothing Then

        htmlText = WebBrowser1.Document.Body.InnerHtml

        If InStr(htmlText, "Microsoft account") Then

            MessageBox.Show("You have entered in a wrong password or the account doesn't exist.")

            'code to go here if it is true
        Else

            MessageBox.Show("Sign in successful. Proceed on...")

            'code to go here if it is false


        End If

    End If

End Sub
Josh Weston
  • 1,632
  • 22
  • 23
  • Wow John! This works very good with the way I have it. However, the only frustration I have is that whenever the program loads it tends to say "Wrong account credentials!" every time the program opens (So I have to click OK) Then I can test the program and it works perfectly (tells me if I have used the wrong credentials or the right). It also prompts me 3 times any time a page is loaded, so I am very confused with that as well. Any ideas to why this is? http://pastebin.com/K8Mrq8FR – Jack C. Mar 26 '13 at 07:06
  • Try adding 'htmlText = Nothing' after the decision has been made (eg. wipe out the contents of the variable). Can you post more code for review? – Josh Weston Mar 28 '13 at 18:26
  • Hmmm... tried that and it didn't work. here is the full code: http://pastebin.com/MmiTJ99j I can't tell what it has to deal with, It's very confusing why it would message box me about entering the wrong credentials 3 times, but when I type in the right code, it only messages me once. – Jack C. Mar 28 '13 at 22:06
  • The issue is with the '.DocumentCompleted' event handler; it fires whenever a frame is complete, not necessarily the whole document. The advisable way is to use the 'ReadyState' method above. Here is a [link](http://stackoverflow.com/questions/2328835/why-is-webbrowser-documentcompleted-firing-twice) to the problem. I would simply change your '.DocumentCompleted' event handler to a function that is called after you navigate to the webpage. – Josh Weston Mar 29 '13 at 01:03
  • Yes I understand but the link you referenced me to is C# and I have no experience with that what-so-ever. – Jack C. Mar 29 '13 at 04:58
  • Yes, it was more for an understanding of the issue then syntactical demonstration. In your code, how often do you want it to check these account credentials? – Josh Weston Mar 29 '13 at 10:40
  • Once. After the log in is done the function is no longer needed (that is, if the credentials are correct). – Jack C. Mar 29 '13 at 14:24