Выпадающие меню с VBA - PullRequest
       19

Выпадающие меню с VBA

2 голосов
/ 19 июня 2019

Мне нужно выбрать конкретную опцию из выпадающего меню, используя VBA. Как я могу это сделать?

https://clinicaltrials.gov/ct2/results?cond=&term=Medpace&cntry=&state=&city=&dist= ссылка на веб-страницу, которую мы пытаемся извлечь из

IE.document.getElementsByName ("down_count"). Нажмите код, который я пробовал

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

1 Ответ

1 голос
/ 19 июня 2019

Ниже показано, как загрузить

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
...