如何在VBA中单击<按钮>

时间:2019-06-19 19:51:02

标签: excel vba internet-explorer web-scraping queryselector

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

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

需要单击“显示/隐藏列”,然后选择“研究类型”“阶段”“赞助者/合作者”“已注册编号”“ NCT编号”“研究开始”“研究完成”“最新更新已发布”

.getElementsByClassName(“ dt-button按钮-集合按钮-colvis”)。单击

私人子工作簿_Open()     昏暗的IE作为对象     设置IE = CreateObject(“ InternetExplorer.application”)     使用IE         .Visible = True         .Navigate(“ https://clinicaltrials.gov/ct2/results?cond=&term=Medpace&cntry=&state=&city=&dist=”)         而.Busy或.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

结束子

1 个答案:

答案 0 :(得分:2)

在数组中具有其他所需的选项,然后循环所有按钮,以检查按钮的innerText是否在数组中。如果已设置类别名称,则该按钮将处于活动状态

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