登录网站

时间:2017-06-03 20:26:20

标签: excel vba excel-vba internet-explorer

我正在尝试创建一个Excel宏,它会将我登录到this website。我已经编写了宏来登录我的其他网站,但这个有点不同。

当我使用以下代码以编程方式插入用户名和密码,然后以编程方式单击“登录”按钮时,网页将返回消息

  

错误:请提供用户名和错误:请提供   密码

Sub LogIn()
' Open IE and navigate to the log-in page    
    Set ie = CreateObject("InternetExplorer.Application")

    With ie
        .Visible = True
        .navigate "https://www.prudential.com/login"

' Loop until the page is fully loaded
        Do Until Not ie.Busy And ie.ReadyState = 4
            DoEvents
        Loop
        Application.Wait (Now + TimeValue("0:00:10"))

' Enter the necessary information on the Login web page and click the submit button
        ie.document.getElementById("username").Value = "abcdef"
        ie.document.getElementById("password").Value = "12345678"
        ie.document.getElementsByClassName("btn btn-primary btn-login btn-sm-block analytics-login")(0).Click

' Loop until the page is fully loaded
        Do Until Not ie.Busy And ie.ReadyState = 4
             DoEvents
        Loop
    End With

' Do stuff

' Quit IE
    ie.Quit
End Sub

我可以看到我的代码已将用户名和密码插入到网页框中,但由于某些原因,在成功点击提交按钮后,未检测到用户名和密码。

哪些代码会成功插入用户名和密码,以便网页处理? 当您收到以下错误消息时,您将知道您的代码有效:

  

我们无法验证您的用户名和密码。请再试一次。

感谢您的帮助!

4 个答案:

答案 0 :(得分:5)

它实际上希望您发送密钥,因此您可以绕过此请求:

View

修改

ie.document.getElementById("username").Focus
ie.document.getElementById("username").Value = "abcde"
Application.SendKeys "f", True

ie.document.getElementById("password").Focus
ie.document.getElementById("password").Value = "1234567"
Application.SendKeys "8", True

ie.document.getElementsByClassName("btn btn-primary btn-login btn-sm-block analytics-login")(0).Click

答案 1 :(得分:2)

另一种方法可能如下所示。

Sub Get_Logged_In()
    Dim IE As New InternetExplorer, HTML As HTMLDocument
    Dim post As Object, elem As Object

    With IE
        .Visible = True
        .navigate "https://www.prudential.com/login"
        While .Busy = True Or .readyState < 4: DoEvents: Wend
        Set HTML = .document
    End With

    With HTML
        Set post = .querySelector("#username")
        post.Focus
        post.innerText = "abcdef"

        Set elem = .querySelector("#password")
        elem.Focus
        elem.innerText = "12345678"

        .querySelector("button[data-qa='Login Button']").Click
    End With
    IE.Quit
End Sub

参考添加到库:

1. Microsoft Internet Controls
2. Microsoft HTML Object Library

答案 2 :(得分:1)

尝试以下代码:

Option Explicit

Const READYSTATE_COMPLETE       As Long = 4
Const URL_String                As String = "https://www.prudential.com/login"

Sub Login()

    Dim IE As Object
    Dim ObjElement As Object

    ' using Late Binding
    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .navigate URL_String
        .Visible = True
    End With

    Do While (IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE)
        DoEvents
    Loop

    IE.document.getElementByid("username").Value = "abcdef"
    IE.document.getElementByid("password").Value = "12345678"

    ' Optional: I didn't need the wait time
    Application.Wait (Now + TimeValue("0:00:01"))

    Set ObjElement = IE.document.getElementsByClassName("btn btn-primary btn-login btn-sm-block analytics-login")
    ObjElement(0).Click

    Set IE = Nothing

End Sub

答案 3 :(得分:0)

如果其他人遇到类似的问题我终于能够让Sendkeys工作了。但是,如果任何人都可以提出非Sendkey解决方案,那将更受欢迎。

使Sendkeys工作的两个重点是1)使用

    Set WSS = CreateObject("WScript.Shell")

和2)确保从Excel运行宏(例如 tools-macro-macros)和VB编辑器中的 not (从编辑器运行将简单将文本插入代码中的某个位置而不是目标网页上。

' Open IE and navigate to the log-in page
    Application.StatusBar = "Opening IE and logging in to the website"

    Set WSS = CreateObject("WScript.Shell")
    my_username = "abcdef"
    my_pw = "12345678"

    Set ie = CreateObject("InternetExplorer.Application")

    With ie
        .Visible = True
        .navigate "https://www.prudential.com/login"
        .Top = 0
        .Left = 0
        .Height = 1025
        .Width = 1925

' Loop until the page is fully loaded
        Do Until Not ie.Busy And ie.ReadyState = 4
            DoEvents
        Loop
        Application.Wait (Now + TimeValue("0:00:10"))        
        WSS.SendKeys "{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}"
        Application.Wait (Now + TimeValue("0:00:01"))

        WSS.SendKeys my_username
        Application.Wait (Now + TimeValue("0:00:01"))
        WSS.SendKeys "{TAB}"
        Application.Wait (Now + TimeValue("0:00:01"))
        WSS.SendKeys my_pw
        Application.Wait (Now + TimeValue("0:00:01")

        ie.document.getElementsByClassName("btn btn-primary btn-login btn-sm-block analytics-login")(0).Click

' Loop until the page is fully loaded
        Do Until Not ie.Busy And ie.ReadyState = 4
             DoEvents
        Loop
    End With

' You're now logged in, do your stuff..