如何点击 VBA 中的 <button>

How to click a <button> in VBA

我需要帮助单击按钮,然后使用 VBA 在网页上 selecting 选项。

网页Link:https://clinicaltrials.gov/ct2/results?cond=&term=Medpace&cntry=&state=&city=&dist=

我需要点击 "Show/Hide Columns" 然后 select "Study Type", "Phase", "Sponsor/Collaborators", "Number Enrolled", "NCT Number", "Study Start"、"Study Completion" 和 "Last Update Posted"。

"Show/Hide Columns" 按钮的 class:.getElementsByClassName("dt-button buttons-collection buttons-colvis").click

Private Sub Workbook_Open()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.application")
    With IE
        .Visible = True
        .Navigate ("https://clinicaltrials.gov/ct2/results?cond=&term=Medpace&cntry=&state=&city=&dist=")
        While .Busy Or .readyState <> 4: DoEvents: Wend

        With IE.document
            IE.Refresh
            .getElementsByClassName("dt-button buttons-collection buttons-colvis").click
            .querySelector("#save-list-link").click
            .querySelector("#number-of-studies option:last-child").Selected = True
            ' .querySelector("#which-format option:fourth-child").Selected = True
            ' .querySelector("#which-format").selectedIndex = 3
            ' .querySelector ("#number-of-studies").selectedIndex = 1
            ' .querySelector("[value=csv]").click
            .querySelector("#submit-download-list").click

        ' Set div = IE.document.getElementById("save-list-link")
        ' div.FireEvent "onclick"
        End With
        Application.Wait Now + TimeSerial(0, 0, 10)
        Application.SendKeys "%+s", True
        Application.Wait Now + TimeSerial(0, 0, 10)
        .Quit

    ' For Each elt In IE.document.getElementById("number-of-studies")
        ' If InStr(elt.innerText, "Found") > 0 Then elt.click: Exit For
    ' Next elt

    ' Set div4 = IE.document.getElementById("submit-download-list")
    ' div4.click
    End With
End Sub

将您需要的其他选项放入数组中,然后循环所有按钮,检查按钮的 innerText 是否在数组中。如果是,则设置 class 名称,以便按钮处于活动状态

Option Explicit
Public Sub MakeSelections()
    Dim ie As Object, options()
    options = Array("Study Type", "Phase", "Sponsor/Collaborators", "Number Enrolled", "NCT Number", "Study Start", "Study Completion", "Last Update Posted")
    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .Navigate2 "https://clinicaltrials.gov/ct2/results?cond=&term=Medpace&cntry=&state=&city=&dist="

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
           .querySelector(".buttons-collection").Click 'show/hide
           Dim buttons As Object, i As Long
           Set buttons = .querySelectorAll(".two-column button")
           For i = 0 To buttons.Length - 1
               If Not IsError(Application.Match(Trim$(buttons.item(i).innerText), options, 0)) Then
                   buttons.item(i).className = "dt-button buttons-columnVisibility active"
               End If
           Next
        End With
        Stop
        .Quit
    End With
End Sub