【问题标题】:VBA to click on buttonVBA点击按钮
【发布时间】:2019-09-28 16:13:48
【问题描述】:

使用 VBA 的 Internet Explorer 按钮的问题。

我需要有关使用 vba 单击按钮的帮助。问题是当我自动输入用户名和密码时,该按钮不可点击。只有当我手动输入用户名和密码时它才可点击。我的代码适用于网站上除此之外的其他按钮。

'这是我的代码中输入用户名、密码并点击按钮的部分。

    Set HTMLInput = HTMLDoc.getElementById("USER")
    HTMLInput.Value = "user"

    Set HTMLInput = HTMLDoc.getElementById("Password")
    HTMLInput.Value = "password"

    Set HTMLButton= HTMLDoc.getElementById("subBtn")
    HTMLButton.Click

'这是按钮信息

<button type="submit" class="btn btn-primary ng-scope" data-ng-click="saveUserID()" id="subBtn" translate="signin.signin">Sign In</button>

我在某处读到过,我可以使用Application.sendkeys"username",True 来模拟键盘上的输入以查看按钮是否可点击,但我也无法让它工作。还有其他方法可以使此按钮可点击吗?

【问题讨论】:

  • .Click 不起作用时,有几种方法可以做到这一点。正确的方法取决于表单的设置方式。例如,您可以触发 onclick 事件,如 this 或者您可以 .submit() 表单本身类似于 this
  • 可以分享一下网址吗?

标签: html vba internet-explorer web-scraping automation


【解决方案1】:

将 disabled 属性更改为 False

Option Explicit
'VBE > Tools > References: Microsoft Internet Controls
Public Sub Login()
    Dim ie As New InternetExplorer
    With ie
        .Visible = True
        .Navigate2 "https://workforcenow.adp.com/workforcenow/login.html"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            .querySelector("#user_id").Value = "something"
            .querySelector("#password").Value = "somethingElse"

            With .querySelector("#subBtn")
                .disabled = False
                .Click
            End With
            Stop '< delete me later
        End With

        While .Busy Or .readyState < 4: DoEvents: Wend
        'other stuff post login
        .Quit
    End With
End Sub

【讨论】:

  • 我实际上找到了如何使用 Matteo NNZ 答案的一部分来做到这一点,但由于你的答案也有效,我选择它作为最佳答案。感谢您抽出宝贵时间帮助我!
【解决方案2】:

尝试让网站认为您输入的是真实的这些值。 例如:

Set HTMLInput = HTMLDoc.getElementById("USER")
HTMLInput.Focus '<-- set the focus on the input (make the element the active one)
Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1) '<-- wait a second
HTMLInput.Value = "user"

Set HTMLInput = HTMLDoc.getElementById("Password")
HTMLInput.Focus '<-- set the focus on the input (make the element the active one)
Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1) '<-- wait a second
HTMLInput.Value = "password"

Set HTMLButton= HTMLDoc.getElementById("subBtn")
HTMLButton.Click '<-- finally click the button

注意:由于您没有共享 URL,因此此代码未经测试。但它过去在某些基于 JavaScript 的网站上对我有用。

【讨论】:

    【解决方案3】:

    我就是这样解决的。不确定这是否是解决它的最佳方法,但它有效。 sendkeys 命令实际上使它认为您正在手动输入 id 和密码。我需要添加等待时间,因为它会快速跳到密码并且没有完成输入 user_id。谢谢你们花时间帮助我!

        HTMLInput.Focus '<-- set the focus on the input (make the element the active one)
        Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1) '<-- wait a second
        SendKeys "user"
        Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1) '<-- wait a second
    
        Set HTMLInput = HTMLDoc.getElementById("password")
        HTMLInput.Focus '<-- set the focus on the input (make the element the active one)
        Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1) '<-- wait a second
        SendKeys "password"
        Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1) '<-- wait a second
    
        Set HTMLbutton = HTMLDoc.getElementById("subBtn")
        HTMLbutton.Click '<-- finally click the button```
    

    【讨论】:

      猜你喜欢
      • 2015-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-22
      • 1970-01-01
      • 2016-05-19
      • 2013-07-26
      • 2019-10-25
      相关资源
      最近更新 更多