Excel VBA 单击没有名称或 ID 使用 JSON 的网站按钮

Excel VBA click website button with no name or ID that uses JSON

我正在使用 excel VBA 打开这个网站: Legal and General, 然后需要点击"Fund prices and charges"按钮。

用chrome检查网页,我可以看到这个按钮有以下代码:

<div class=" selected tab" tabindex ="0" role="button" aria-pressed="true">...</div>

HTML'view source'提示脚本类型="application/JSON"

我对这一切感到很困惑。有谁知道我如何 select 这个按钮?到目前为止我有这部分代码:

 Set HTMLDoc = .document

Set objElement = HTMLDoc.getElementsByClassName("Select - Tab - Fund prices and charges")(0)

    objElement.Click

.Quit

任何帮助将不胜感激。

此致

戴夫

如果只对那个选项卡感兴趣,我会首先获取包含所有选项卡的父元素 (div),使用 css [=] class名称 class 选择器。然后,我将结合使用子组合器和 nth-child() 选择器来通过其 classname:

获取第二个子 div
.querySelector(".table-view__tabs > div:nth-child(2)").Click 'Select tab direct

如果可能对所有 3 个标签感兴趣:

我会首先使用 css class 选择器通过其 class 名称获取包含所有选项卡的父元素 (div)。然后,我将使用子组合器通过 class 名称获取子 divs(即选项卡)。我将索引到返回的 nodeList 以单击所需的选项卡:

Set tabs = .querySelectorAll(".table-view__tabs > div") 'select all tabs then index in for tab of choice
tabs.Item(1).Click       

VBA:

Option Explicit

Public Sub ClickButton()

    Dim ie As SHDocVw.InternetExplorer

    Set ie = New SHDocVw.InternetExplorer

    With ie
        .Visible = True
        .Navigate2 "https://fundcentres.lgim.com/uk/workplace-employee/fund-centre/#Product=WorkSave-Pension-Plan-(generation-3)"

        While .Busy Or .ReadyState <> 4: DoEvents: Wend

        With .Document

            .querySelector(".table-view__tabs > div:nth-child(2)").Click 'Select tab direct

            Stop  'Delete me later

            Dim tabs As Object

            Set tabs = .querySelectorAll(".table-view__tabs > div") 'select all tabs then index in for tab of choice
            tabs.Item(1).Click                   '0 based so first tab is 0, second is 1 etc

            Stop                                 'Delete me later

        End With

        Stop                                     '<==Delete me later
        .Quit
    End With
End Sub

正在阅读:

  1. CSS selectors
  2. Child combinator
  3. Class selector
  4. :nth-child
  5. document.querySelectorAll
  6. document.querySelector

参考(VBE>工具>参考):

  1. Microsoft Internet 控件