【问题标题】:VBA Click Through Website MenuVBA 点击网站菜单
【发布时间】:2017-10-18 15:19:14
【问题描述】:

我想创建一些 VBA 来从网站中提取信息。我在 HTML 中找到了这些信息,但我相信只有在页面上单击某些按钮时才会在 HTML 源代码中显示/填充它。我一直在尝试使用 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>

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

欢迎任何想法!

詹姆斯

【问题讨论】:

    标签: html vba web-scraping screen-scraping


    【解决方案1】:

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

    我会使用以下内容:

    第一个按钮有一个 id,所以你可以尝试一下:

    .document.getElementById("otherActionsButton").Click
    

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

    [data-qa='show-history']
    

    你申请如下:

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

    【讨论】:

    • 非常感谢,QHarr!没想到会得到答案,但这意味着我可以再接再厉:)
    猜你喜欢
    • 1970-01-01
    • 2017-05-10
    • 2018-12-26
    • 2022-01-13
    • 2020-07-24
    • 1970-01-01
    • 2021-03-31
    • 2018-12-31
    • 1970-01-01
    相关资源
    最近更新 更多