【问题标题】:Clicking a button in Internet Explorer using VBA (getElementsByClassName)使用 VBA (getElementsByClassName) 在 Internet Explorer 中单击一个按钮
【发布时间】:2017-11-14 21:20:02
【问题描述】:

我正在尝试使用 VBA 和 HTML 在 Internet Explorer 窗口中单击一个按钮。该按钮没有“id”,所以我必须通过“类名”找到它。

按钮的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

【问题讨论】:

    标签: html excel vba getelementsbyclassname


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      getElementsByClassName 返回一个集合。您将需要访问第一个索引,或遍历集合。

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

      【讨论】:

      • 所以我知道 getElementById 与 getElementsByClassName 的不同之处在于后者返回一个数组并且我需要访问该类名的第一个索引,但是在我选择了正确的之后如何实际单击按钮索引
      • (1) 是 VBA 的东西吗?
      • @elfwyn 是的,这就是您获取数组中第一个元素的方式。
      • 好的...删除了我的答案,因为我不能确定它是否与 VBA 兼容。
      • @Davey .Click 很好。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 2020-05-01
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多