2016-06-28 8 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

私は が(それは私のために働いていた)これを試して、同じエラーが発生しました:

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

関連する問題