2016-06-28 5 views
0

VBA 및 HTML을 사용하여 Internet Explorer 창에서 단추를 클릭하려고합니다. 버튼에는 "id"가 없으므로 "classname"으로 찾아야합니다. 다음과 같이VBA (getElementsByClassName)를 사용하여 Internet Explorer에서 버튼을 클릭하십시오.

버튼의 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

저도 같은 오류를 가지고, (이것은 나를 위해 일한)이 시도 해당 클래스 이름의 첫 번째 인덱스,하지만 올바른 인덱스를 선택한 후 실제로 어떻게 단추를 클릭합니까
0

getElementsByClassName이 컬렉션을 반환합니다. 첫 번째 색인에 액세스하거나 컬렉션을 반복해야합니다.

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

은 그래서에서 getElementById가 배열 점에서 getElementsByClassName에서 후자의 수익을 다른 것을 이해하고 내가 액세스 필요 :

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

+0

(1) VBA 것은 무엇입니까? – elfwyn

+0

@elfwyn 예, 배열에서 첫 번째 요소를 얻는 방법입니다. – 4castle