使用webbrowser1的vb.net自动登录

问题描述

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

点击网页上的“ 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

解决方法

在进一步测试并更正了我的新代码之后,我能够完成登录过程。这是带有一串“ 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...