我试图使用VBA点击"登录"网站上的按钮自动登录

时间:2016-12-01 23:02:07

标签: vba internet-explorer automation ie-automation

我一直在尝试使用VBA自动提交登录凭据,我可以填写用户名和密码字段,但我无法获取代码来点击登录按钮。我已尝试过在此处以及其他网站上找到的众多解决方案,但无法使其发挥作用。这是我的代码(我已经尝试点击按钮):

Ext.onReady(function() {
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'side';
    document.body.style.overflow = "hidden";

    var tb = new Ext.Toolbar({
        items: [{
            xtype: 'splitbutton',
            id: 'setLanguage',
            text: 'English',

            menu: new Ext.menu.Menu({
            items: [
                    {text: 'English', id: 'EN', handler: changeLanguage},
                    {text: 'Español', id: 'ES', handler: changeLanguage},
                    {text: 'Deutsch', id: 'DE', handler: changeLanguage},
                    {text: 'Français', id: 'FR', handler: changeLanguage},
                    {text: 'Nederlands', id: 'NL', handler: changeLanguage}
                ]
            })
        }, '->', {
            text: 'Logon',
            id: 'Logon',
            disabled: true,
            handler: performLogon,
            formBind: true
        }]
    });

    var logonForm = new Ext.FormPanel({
        labelWidth: 100,
        frame: true,
        title: 'Member Logon',
        id: 'LogonForm',
        bodyStyle: 'padding: 5px 5px 0',
        width: 350,
        defaults: {width: 200},
        defaultType: 'textfield',
        floating: true,
        shadow: 'drop',
        shadowOffset: 15,
        monitorValid: true,
        buttonAlign: 'left',

        items: [{
                fieldLabel: 'User Name',
                id: 'UserName',
                name: 'UserName',
                allowBlank: false,

                maxLength: 50
            },{
                fieldLabel: 'Password',
                id: 'Password',
                name: 'Password',
                inputType: 'password',
                maxLength: 20
            }, new Ext.form.Checkbox({

                fieldLabel: '',
                labelSeparator: '',
                id: 'RememberMe',
                name: 'RememberMe',
                boxLabel: 'Remember Me?',
                style: 'margin-right: 8px',
                checked: getCookie('REMEMBERME') == "true" ? true : false
            })],

        fbar: tb

以下是我认为登录的一些源代码:

$('.inputmask_alphanumeric').inputmask({ mask: "[*]{100}", greedy: false });

1 个答案:

答案 0 :(得分:0)

要执行单击,您只需要在Logon按钮元素上调用Click方法,如下所示:

Call HTMLDoc.all.Logon.Click

然而,问题是,网站仅在您输入用户名和密码后才启用该按钮。如果您在填写用户名和密码后立即尝试单击按钮,这太早了,按钮仍处于禁用状态,因此您需要等待片刻。

为了在VBA中等待,您可以像这样导入WinAPI Sleep函数:

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

然后等待一段给定的毫秒数:

Sleep (500)

合并代码:

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub Demo()
  Dim MyURL As String
  Dim MyBrowser As InternetExplorer
  Dim HTMLDoc As HTMLDocument

  MyURL = "https://www.oms.itradenetwork.com/secure/login/logon.cfm"
  Set MyBrowser = New InternetExplorer
  MyBrowser.Silent = True
  MyBrowser.Navigate MyURL
  MyBrowser.Visible = True

  Do
    DoEvents
  Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE

  Set HTMLDoc = MyBrowser.Document
  HTMLDoc.all.UserName.Value = "MyName"
  HTMLDoc.all.Password.Value = "MyPassword"

  ' First wait 500ms to give the webite enough time to enable the button
  Sleep (500)
  ' then click the button
  Call HTMLDoc.all.Logon.Click
End Sub