Excel 如何单击a<;按钮>;在VBA中

Excel 如何单击a<;按钮>;在VBA中,excel,vba,internet-explorer,web-scraping,queryselector,Excel,Vba,Internet Explorer,Web Scraping,Queryselector,我需要帮助单击一个按钮,然后使用VBA在网页上选择选项 网页链接: 我需要单击“显示/隐藏列”,然后选择“研究类型”、“阶段”、“赞助者/合作者”、“登记人数”、“NCT人数”、“研究开始”、“研究完成”和“最近更新发布” “显示/隐藏列”按钮的类:.getElementsByClassName(“dt按钮”“按钮集合”“按钮colvis”)。单击 Private Sub Workbook_Open() Dim IE As Object Set IE = CreateObject

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

网页链接:

我需要单击“显示/隐藏列”,然后选择“研究类型”、“阶段”、“赞助者/合作者”、“登记人数”、“NCT人数”、“研究开始”、“研究完成”和“最近更新发布”

“显示/隐藏列”按钮的类:
.getElementsByClassName(“dt按钮”“按钮集合”“按钮colvis”)。单击

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
Private子工作簿\u Open()
模糊的物体
设置IE=CreateObject(“InternetExplorer.application”)
与IE
.Visible=True
.导航(“https://clinicaltrials.gov/ct2/results?cond=&term=Medpace&cntry=&state=&city=&dist=")
忙时或准备时状态4:DoEvents:Wend
用IE文件
更新
.GetElementsByCassName(“dt按钮集合按钮colvis”)。单击
.querySelector(“保存列表链接”)。单击
.querySelector(“#研究次数选项:最后一个孩子”)。Selected=True
“.querySelector(#哪种格式选项:第四个子项”)。Selected=True
“.querySelector(“#哪种格式”)。selectedIndex=3
“.querySelector(#研究数量”)。selectedIndex=1
“.querySelector(“[value=csv]”)。单击
.querySelector(“提交下载列表”)。单击
'Set div=IE.document.getElementById(“保存列表链接”)
'div.firevent“onclick”
以
应用程序。立即等待+时间序列(0,0,10)
Application.SendKeys“%+s”,真
应用程序。立即等待+时间序列(0,0,10)
退出
'对于IE.document.getElementById(“研究数量”)中的每个elt
'如果InStr(elt.innerText,“Found”)>0,则elt.click:退出
“下一个
'Set div4=IE.document.getElementById(“提交下载列表”)
'div4.click
以
端接头

将所需的附加选项放入数组中,然后循环所有按钮,检查按钮的内部文本是否在数组中。如果是,则设置类名,使按钮处于活动状态

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
选项显式
公共子生成选项()
将ie设置为对象,选项()
选项=数组(“研究类型”、“阶段”、“赞助者/合作者”、“登记人数”、“NCT人数”、“研究开始”、“研究完成”、“最近发布的更新”)
设置ie=CreateObject(“InternetExplorer.Application”)
与ie
.Visible=True
.导航2“https://clinicaltrials.gov/ct2/results?cond=&term=Medpace&cntry=&state=&city=&dist="
当.Busy或.readyState<4:DoEvents:Wend时
随附.文件
.querySelector(“.buttons集合”)。单击“显示/隐藏”
暗按钮为对象,我为长按钮
设置按钮=.querySelectorAll(“.2列按钮”)
对于i=0的按钮,长度为-1
如果不是IsError(Application.Match(Trim$(buttons.item(i).innerText),options,0),那么
buttons.item(i).className=“dt button buttons columnVisibility active”
如果结束
下一个
以
停止
退出
以
端接头

为什么不使用链接或数据下载?