VBA 点击浏览网站菜单

VBA Click Through Website Menu

我想创建一些 VBA 来从网站中提取信息。我在 HTML 中找到了信息,但我相信当在页面上单击某些按钮时,它只是 HTML 源中的 shown/populated。我一直在尝试使用 getElementsByTagName 来执行点击,但到目前为止还没有奏效。到目前为止,我的代码是立即尝试第二次点击:

Sub googlesearch()
    Set objIE = CreateObject("InternetExplorer.Application")
    WebSite = "MyLink"

    With objIE
        .Visible = True
        .navigate WebSite
        Do While .Busy Or .readyState <> 4
            DoEvents
        Loop

Set elems = .getElementsByTagName("button")
    For Each e In elems

        If (e.getAttribute("id") = "show-history") Then
            e.Click
            Exit For
        End If

    Next e

End With

 Set ie = Nothing

End Sub

我收到 运行 时间错误 438,对象不支持 属性 或方法。

我要先点击的元素代码是:

<button class="btn btn-lg btn-trigger other-actions-btn" id="otherActionsButton" aria-haspopup="true" type="button" data-qa="toggle-other-actions" olive-menu-initialized="true" olive-menu-trigger="otherActionsMenu" olive-menu-position="below right" olive-menu="otherActionsMenu">Other Actions</button>

第二个按钮的代码是:

<button class="item button-link" type="button" data-qa="show-history" data-action="show:history">View History</button>

由于后台发生的事情,页面加载和所有元素可点击之间有时似乎存在延迟,我不确定这是否会导致问题?

欢迎任何想法!

詹姆斯

首先,您尝试使用的方法不是 IE 的一部分,而是 .document 的一部分,因此您需要 .document.getElementsByTagName("button")

我会使用以下内容:

第一个按钮有一个 ID,因此您可以尝试:

.document.getElementById("otherActionsButton").Click

第二个按钮,您可以使用 attribute=value CSS 选择器通过其值定位 data-qa 属性:

[data-qa='show-history']

您申请如下:

.document.querySelector("[data-qa='show-history']").Click