首页 > 解决方案 > vb.net 使用 webbrowser1 自动登录

问题描述

尝试使用 vb.net 表单应用程序自动登录到网站。我没有收到任何异常或错误。我导航到我的页面,设置电子邮件和密码属性,然后单击网页上的“cmd”按钮。使用调试语句,我已经验证了属性已设置。

在我点击网页上的“cmd”按钮后,我回到了我开始时的同一页面,但找不到任何告诉我有异常或错误的信息。

我可以使用相同的电子邮件/密码组合使用 chrome 或 IE 登录网页。

这是我的代码。

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

标签: vb.netautologin

解决方案


在进一步测试和更正我的新代码后,我能够完成登录过程。这是带有一堆“debug.writeline”语句的代码。

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

推荐阅读