IE登录自动化 - 如何按提交按钮?

时间:2014-12-04 20:24:49

标签: excel vba internet-explorer email login

我正在尝试使用IE作为浏览器登录电子邮件帐户,这是使用excels VBA环境自动完成的,到目前为止我有以下内容:

    Option Explicit

Sub login()
    Const cURL = "https://login.microsoftonline.com/"
    Const cUsername = "XXXX"    'REPLACE XXXX WITH YOUR USER NAME
    Const cPassword = "YYYY"    'REPLACE YYYY WITH YOUR PASSWORD

    Dim IE As InternetExplorer
    Dim doc As HTMLDocument
    Dim LoginForm As HTMLFormElement
    Dim UserNameInputBox As HTMLInputElement
    Dim PasswordInputBox As HTMLInputElement
    Dim SignInButton As HTMLInputButtonElement
    Dim HTMLelement As IHTMLElement

    Set IE = New InternetExplorer
    IE.Visible = True
    IE.Navigate cURL

    'Wait for initial page to load
    Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop

    Set doc = IE.Document

    'Get the only form on the page
    Set LoginForm = doc.forms(0)

    'Get the User Name textbox and populate it
    Set UserNameInputBox = LoginForm.elements("cred_userid_inputtext")
    UserNameInputBox.Value = cUsername

    'Get the password textbox and populate it
    Set PasswordInputBox = LoginForm.elements("cred_password_inputtext")
    PasswordInputBox.Value = cPassword

    'Get the form input button and click it
    Set SignInButton = LoginForm.elements("cred_sign_in_button")
    SignInButton.Click

    'Wait for the new page to load
    Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop

    Debug.Print "Current URL: " & IE.LocationURL

End Sub

使用此代码,用户名和密码输入正常,但当我尝试按&#34;提交&#34;按钮,我得到一个运行时错误,不知道为什么,我认为它可能是一个JavaScript对象但是,无法弄清楚如何让VBA按下它。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果LoginForm项是真正的html'form'元素,那么它应该有一个submit方法。在这种情况下LoginForm.Submit可以做到这一点

相关问题