首页 > 解决方案 > VBA点击按钮

问题描述

使用 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来模拟键盘上的打字,看看按钮是否可以点击,但我也无法让它工作。还有其他方法可以使此按钮可点击吗?

标签: htmlvbainternet-explorerweb-scrapingautomation

解决方案


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

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 的网站上对我有用。


推荐阅读