0

Trying to autologin to a web site using a vb.net form application. I do not receive any exceptions or errors. I Navigate to my page, set the email and password attributes and then click the 'cmd' button on the webpage. Using Debug statements I have verified the attributes are set.

After I click the 'cmd' button on the webpage I get back the same page I started with and cannot find anything that tells me there was an exception or error.

I am able to login to the webpage using chrome or IE using the same email/password combination.

Here is my code.

Private mPageReady As Boolean = False

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

    AddHandler Me.WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)

End Sub

Public Function LoginUser(pEmailAddress As String, pPassword As String) As Boolean
    Dim IsOkay As Boolean = False
    Dim myURL As String = "https://login.wlpc.com/index.php/"


    Me.WebBrowser1.Navigate(myURL)
    WaitForPageLoad()
    Debug.WriteLine("After Navigate: " & Me.WebBrowser1.DocumentText)

    Try
        Me.WebBrowser1.Document.GetElementById("email").SetAttribute("value", pEmailAddress)
        Debug.WriteLine("After assignment of email: " & Me.WebBrowser1.Document.GetElementById("email").GetAttribute("value"))
        Me.WebBrowser1.Document.GetElementById("password").SetAttribute("value", pPassword)
        Debug.WriteLine("After assignment of password: " & Me.WebBrowser1.Document.GetElementById("password").GetAttribute("value"))

        Dim myDoc As HtmlDocument = Me.WebBrowser1.Document
        Dim myCmd As HtmlElement = myDoc.All("cmd")
        myCmd.InvokeMember("click")
        WaitForPageLoad()

        Debug.WriteLine("After click: " & Me.WebBrowser1.DocumentText)
        IsOkay = True
    Catch ex As Exception
        IsOkay = False
    End Try

    Return IsOkay
End Function

Public Function GetPage(URL As String) As String

    Debug.WriteLine(String.Format("Accessing {0}", URL))

    Me.WebBrowser1.Navigate(URL)
    WaitForPageLoad()

    Dim pagedata As String = Me.WebBrowser1.DocumentText

    Return pagedata

End Function

Public Sub WaitForPageLoad()
    While Not mPageReady
        Application.DoEvents()
    End While

    mPageReady = False
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If Me.WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        mPageReady = True
    End If
End Sub

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

    LoginUser(mEmailAddress, mPassword)

End Sub
Steve
  • 11
  • 1
  • 2
  • I don't see how this could work: the `email` element has no ID (so you won't find it), the `password` element has an ID, but it's `password1`, not `password` (so you won't find it). See, for example, here: [How to fill a WebForm and click the submit Button with a WebBrowser control?](https://stackoverflow.com/a/64668045/7444103). See the notes: you need to implement what's described in the *WebBrowser Emulation Advanced Features* part. Then follow the procedure described there, changing the parameters used there with yours. ► Remove any occurrence of `Application.DoEvents()` from your code. – Jimi Nov 12 '20 at 04:37
  • My changes to match your suggestion did not result in any improvement. I can drop in the new code if you want to see it. I can drop in the new code if you want to review it. – Steve Nov 12 '20 at 06:12
  • Well, if you changed your code then for sure post the updated code. -- Did you enable the WebBrowser Emulation Advanced features shown [here](https://stackoverflow.com/a/57615713/7444103)? – Jimi Nov 12 '20 at 12:01
  • I am trying to modify my code to include what is needed from the WebBrowser Emulation Advanced Features part. Not having much luck getting the ActivatWBAdvancedFeatures to execute. Keep getting an exception of Object reference not set to an instance of an object. wbAccelKey was nothing. – Steve Nov 12 '20 at 18:34
  • Because that Key may not exist, you need to create it if it's not there. Anyway, I've modified that code to include this step. – Jimi Nov 12 '20 at 19:18

1 Answers1

0

After further testing and a correction to my new code, I was able to complete the login process. Here is the code with a bunch of 'debug.writeline' statements.

Private Sub BtnSignIn_Click(sender As Object, e As EventArgs) Handles BtnSignIn.Click
    Dim myURL As String = "https://login.wlpc.com/index.php/"

    AddHandler Me.WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf WebBrowserDocumentCompleted)
    Me.WebBrowser1.Navigate(myURL)
End Sub


Private Sub WebBrowserDocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)

    If Me.WebBrowser1.ReadyState <> WebBrowserReadyState.Complete Then
        Debug.WriteLine(Me.WebBrowser1.ReadyState.ToString)
        Return
    End If

    Try

        Const lEmailAddress As String = "TestEmail@somewhere.com"
        Const lThisPassword As String = "SimplePassword"

        ' get the form:   <form action="/index.php" method="POST" name="login">
        Dim lFormHtmlElement As HtmlElement = Nothing       ' the form element
        Dim lFormFound As Boolean                           ' can we find the form?

        For Each lFormHtmlElement In WebBrowser1.Document.Forms()
            If lFormHtmlElement.Name = "login" Then
                lFormFound = True
                Exit For
            End If
        Next

        If Not lFormFound Then
            Debug.WriteLine("Can't find form")
            Exit Sub
        End If

        '<input type="email" name="email" value="" tabindex="1" placeholder="name@example.com">
        Dim lEmail As HtmlElement = lFormHtmlElement.Document.GetElementById("email")
        If IsNothing(lEmail) Then
            Debug.WriteLine("lEmail element is nothing")
            Exit Sub
        Else
            Debug.WriteLine("lEmail element was found")
        End If

        If IsNothing(lEmail.GetAttribute("value")) Then
            Debug.WriteLine("lEmail attribute value is nothing before set")
        Else
            Debug.WriteLine("lEmail attribute value is contains '" & lEmail.GetAttribute("value") & "' before set")
        End If
        lEmail.SetAttribute("value", lEmailAddress)
        If IsNothing(lEmail.GetAttribute("value")) Then
            Debug.WriteLine("lEmail value is nothing after set")
        ElseIf lEmail.GetAttribute("value") = lEmailAddress Then
            Debug.WriteLine("lEmail set to: '" & lEmail.GetAttribute("value") & "'")
        End If

        '<input type="password" name="password" id="password1" size="25" tabindex="2">
        Dim lPassword As HtmlElement = lFormHtmlElement.Document.GetElementById("password")
        Dim lPassword1 As HtmlElement = lFormHtmlElement.Document.GetElementById("password1")

        If IsNothing(lPassword) Then
            Debug.WriteLine("lPassword element is nothing")
            Exit Sub
        Else
            Debug.WriteLine("lPassword element was found")
        End If
        If IsNothing(lPassword1) Then
            Debug.WriteLine("lPassword1 element is nothing")
            Exit Sub
        Else
            Debug.WriteLine("lPassword1 element was found")
        End If
        If lPassword.Document.Body.InnerText = lPassword1.Document.Body.InnerText Then
            Debug.WriteLine("lPassword and lPassword1 same body innertext")
        Else
            Debug.WriteLine("lPassword and lPassword1 have different body innertext")
        End If

        If IsNothing(lPassword.GetAttribute("value")) Then
            Debug.WriteLine("lPassword attribute value is nothing before set")
        Else
            Debug.WriteLine("lPassword attribute value is contains '" & lPassword.GetAttribute("value") & "' before set")
        End If
        If IsNothing(lPassword1.GetAttribute("value")) Then
            Debug.WriteLine("lPassword1 attribute value is nothing before set")
        Else
            Debug.WriteLine("lPassword1 attribute value is contains '" & lPassword1.GetAttribute("value") & "' before set")
        End If

        lPassword.SetAttribute("value", lThisPassword)
        If IsNothing(lPassword.GetAttribute("value")) Then
            Debug.WriteLine("lPassword attribute value is nothing after set")
            Exit Sub
        ElseIf lPassword.GetAttribute("value") = lThisPassword Then
            Debug.WriteLine("lPassword set to: '" & lPassword.GetAttribute("value") & "'")
        End If

        If IsNothing(lPassword1.GetAttribute("value")) Then
            Debug.WriteLine("lPassword1 is nothing")
        ElseIf lPassword1.GetAttribute("value") = lThisPassword Then
            Debug.WriteLine("lPassword1 attribute value is same password value as lPassword attribute value")
        End If

        '<button type="submit" name="cmd" value="cred_set" tabindex="3">
        Dim lCmdButton As HtmlElement = lFormHtmlElement.Document.GetElementById("cmd")
        If IsNothing(lCmdButton) Then
            Debug.WriteLine("lCmdButton element is nothing")
        Else
            Debug.WriteLine("lCmdButton element was found")
            ' found the 'cmd' button so click it
            lCmdButton.InvokeMember("click")
        End If

    Catch ex As Exception
        Debug.WriteLine("exception: " & ex.Message)
    Finally

        RemoveHandler Me.WebBrowser1.DocumentCompleted, AddressOf WebBrowserDocumentCompleted

    End Try

End Sub
Steve
  • 11
  • 1
  • 2