且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-09-12 15:52:34

如果仅对该选项卡感兴趣,我将首先使用css类选择器按其类名获取包含所有选项卡的父元素(div).然后,我将结合使用子组合器和nth-child()选择器,以按其类名抓取第二个子div:

If only interested in that one tab I would first grab the parent element (div), which houses all the tabs, by its classname using a css class selector. I would then use a child combinator in combination with nth-child() selector to grab the second child div by its classname:

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

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

If potentially interested in all 3 tabs:

我首先要使用css类选择器按其类名获取包含所有选项卡的父元素(div).然后,我将使用子组合器按其类名来获取子div(即选项卡).我将索引到返回的nodeList中以单击所需的选项卡:

I would first grab the parent element (div), which houses all the tabs, by its classname using a css class selector. I would then use a child combinator to grab the child divs (which are the tabs) by their classnames. I would index into the returned nodeList to click the required tab:

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选择器
  2. 子组合器
  3. 类选择器
  4. :nth-​​child
  5. document.querySelectorAll
  6. document.querySelector
  1. CSS selectors
  2. Child combinator
  3. Class selector
  4. :nth-child
  5. document.querySelectorAll
  6. document.querySelector


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

  1. Microsoft Internet控件