查找 HTML 按钮元素引用(class、标签等)以供 VBA 自动点击?

Finding a HTML button element reference (class, tag, etc) for VBA to automatically click?

我正在尝试从我们使用 HTML 的公司内部网站之一提取数据。我试图点击的按钮是一个 CSV 按钮,但尽管尝试了不同的方法,我还是无法 VBA 找到这个元素。

我可以 VBA 打开网页,但我对 HTML 一无所知,因为我一直使用其他数据拉取方法,

我试过使用 .getElementsByClassName 但我收到一个不支持此方法的对象。

使用 getElementsById 允许我绕过它,但 objElement 不支持 .Click

Sub OpenIE()

Dim IE As InternetExplorerMedium
Set IE = New InternetExplorerMedium

Dim HTMLDoc As HTMLDocument
Dim objElement As HTMLObjectElement

With IE
    .Visible = True
    .Navigate "internal website link"
    While .Busy Or .ReadyState <> READYSTATE_COMPLETE: Wend
    Set HTMLDoc = .document
    Set objElement = HTMLDoc.getElementById("btn btn-default buttons-csv buttons-html5")
    objElement.Click


    .Quit
End With

Set IE = Nothing
End Sub

HTML 按钮代码:

<a class=" "btn btn-default buttons-csv buttons-html5"  tabindex="0" aria-controls="inboundTransfersTable" href="#"><span>CSV</span></a>

最终结果是允许 CSV 作为新 excel workbook/sheet 打开,因为从那里我非常熟悉 sorting/filtering 数据。

对于意大利面条代码表示歉意,因为我由于缺乏知识而不得不在其他线程上找到代码

如有任何帮助,我们将不胜感激

谢谢

尝试在 css 选择器中使用单个 class

HTMLDoc.querySelector(".buttons-csv").click

这个

btn btn-default buttons-csv buttons-html5

是一个复合词 class,每个 space 代表一个新的 class 名称的开始。可能只匹配一个 class (首选)。我选择了 buttons-csv 的可能选项。您可以简单地将 classes 与“.”连接起来。在选择器内部向选择器添加更多 classes,例如

.btn.btn-default

选择器中有两个 class。


确保元素存在的定时循环:

Dim t As Date, ele As Object
Const MAX_WAIT_SEC As Long = 10

t = Timer
Do
    On Error Resume Next
    Set ele = HTMLDoc.querySelector(".buttons-csv")
    On Error GoTo 0
    If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing

If Not ele Is Nothing Then
    ele.Click
End If