2016-06-28 239 views
0

我試圖使用VBA和HTML單擊Internet Explorer窗口中的按鈕。該按鈕沒有「ID」,所以我必須通過「classname」來找到它。使用VBA單擊Internet Explorer中的按鈕(getElementsByClassName)

按鈕的HTML代碼如下:

<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only`" 

VBA代碼:

Private Sub CommandButton21_Click() 

Const cURL = "xxxxxx.com" 
Const cUsername = "xxxxxx" 
Const cPassword = "xxxxxx" 

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 CVBATButton As HTMLInputButtonElement 
Set IE = New InternetExplorer 

IE.Visible = True 
IE.navigate cURL 

Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop 

Set doc = IE.document 



'A previous login window that leads me into a new window, where I need to click a button. 

Set LoginForm = doc.forms(0) 
Set UserNameInputBox = doc.getElementById("BIA_Login") 
UserNameInputBox.Value = cUsername 
Set PasswordInputBox = doc.getElementById("BIA_Pass") 
PasswordInputBox.Value = cPassword 
Set SignInButton = doc.getElementById("login") 
SignInButton.Click 

'It's this portion of the code below that I'm struggling with 

Set doc = IE.document 
Set LoginForm = doc.forms(0) 
Application.Wait Now + TimeValue("00:00:03") 
Set CVBATButton = doc.getElementsByClassName("ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only") 
CVBATButton.Click 

回答

1

我有同樣的錯誤, 試試這個(它的工作對我來說):

Set CVBATButton = doc.getElementsByClassName("ui-button ui-widget ui-state- 
default ui-corner-all ui-button-text-only") 
    For Each btn In CVBATButton 
    btn.Click 
    Exit For 
    Next 
0

getElementsByClassName返回一個集合。您需要訪問第一個索引,或者遍歷集合。

Set CVBATButton = doc.getElementsByClassName("ui-button ui-widget ...")(1) 
CVBATButton.Click 
+0

所以我很理解的getElementById從getElementsByClassName方法的區別在於,後者返回一個數組,我需要訪問該類名的第一個索引,但是如何在選擇正確的索引後實際單擊該按鈕 – Davey

+0

是(1)VBA的事情? – elfwyn

+0

@elfwyn是的,這就是你如何得到數組中的第一個元素。 – 4castle

相關問題