2

我需要使用 VBA 从下拉菜单中选择一个特定选项。我怎样才能做到这一点?

https://clinicaltrials.gov/ct2/results?cond=&term=Medpace&cntry=&state=&city=&dist= 链接到我们试图从中提取的网页

IE.document.getElementsByName("down_count").click 我试过的代码

Full Module:
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

    End With

    With IE.document
    IE.Refresh
        Set div = IE.document.getElementById("save-list-link")
        div.FireEvent "onclick"
    ' Application.SendKeys "{TAB}", True
    ' Application.SendKeys "{TAB}", True
    ' Application.SendKeys "{SPACE}", True
    ' Application.SendKeys "{DOWN}", True
    ' Application.SendKeys "{ENTER}", True
    ' Application.SendKeys "{TAB}", True
    ' Application.SendKeys "{TAB}", True
    ' Application.SendKeys "{SPACE}", True
    ' Application.SendKeys "{DOWN}", True
    ' Application.SendKeys "{DOWN}", True
    ' Application.SendKeys "{DOWN}", True
    ' Application.SendKeys "{ENTER}", True
    IE.document.getElementsByName("down_count").click
    ' 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

希望研究数量转到所有找到(此数字更改)并选择文件格式为 CSV

4

1 回答 1

1

下面为大家介绍下载方法

Option Explicit

'VBE > Tools > References: Microsoft Internet Controls
Public Sub GetData()
    Dim ie As Object
    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("#save-list-link").Click
            .querySelector("#number-of-studies option:last-child").Selected = True 'select last option from that dropdown
            '.querySelector("#number-of-studies").selectedIndex = 1  ''selects 2nd option
            .querySelector("[value=csv]").Selected = True 'select csv with attribute = value css selector

            .querySelector("#submit-download-list").Click
        End With
        Application.Wait Now + TimeSerial(0, 0, 10)
        Application.SendKeys "%+O", True
        Application.Wait Now + TimeSerial(0, 0, 10)
        .Quit
    End With
End Sub
于 2019-06-19T18:34:17.617 回答