1

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

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

我需要点击“显示/隐藏列”,然后选择“学习类型”、“阶段”、“赞助商/合作者”、“注册人数”、“NCT 号码”、“学习开始”、“学习完成”和“最后更新”发布”。

“显示/隐藏列”按钮的类:.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
4

1 回答 1

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
于 2019-06-19T20:34:59.650 回答