2016-12-01 25 views
1

我一直試圖使用VBA自動提交登錄憑證,並且我可以獲取用戶名和密碼字段來填充,但我無法獲取代碼來單擊登錄按鈕。我已經嘗試了很多我在這裏找到的解決方案以及其他網站,並且無法使其工作。這裏是我的代碼(我已經採取了任何試圖點擊該按鈕):我正在嘗試使用VBA單擊網站上的「登錄」按鈕以自動登錄

Dim HTMLDoc As HTMLDocument 
Dim MyBrowser As InternetExplorer 
Sub iTradeLogIn() 

Dim MyHTML_Element As IHTMLElement 
Dim MyURL As String 
On Error GoTo Err_Clear 
MyURL = "https://www.oms.itradenetwork.com/secure/login/logon.cfm? _Key=8C049059F2DD44009E" 
Set MyBrowser = New InternetExplorer 
MyBrowser.Silent = True 
MyBrowser.Navigate MyURL 
MyBrowser.Visible = True 
Do 
Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE 
Set HTMLDoc = MyBrowser.Document 
HTMLDoc.all.UserName.Value = "MyName" 
HTMLDoc.all.Password.Value = "MyPassword" 
'Code Here to click the button 


Err_Clear: 
If Err <> 0 Then 
Err.Clear 
Resume Next 
End If 
End Sub 

下面是一些源代碼,以及在那裏我想登錄爲:

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 
+0

該網站創建與Sencha框架的方式 – NineBerry

回答

0

要執行點擊,你只需要調用Click方法登錄按鈕元素上是這樣的:

Call HTMLDoc.all.Logon.Click 

然而,問題是,該網站僅啓用按鈕,您可以輸入用戶名和後傳字。如果您在填寫用戶名和密碼後立即嘗試單擊該按鈕,則此時間太早,該按鈕仍處於禁用狀態,因此您需要稍等片刻。

爲了在VBA等待,你可以導入W​​inAPI的休眠功能是這樣的:

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 
+0

太棒了!這很好,謝謝! –