1

I need help clicking a button and then selecting options on a webpage using VBA.

Webpage Link: https://clinicaltrials.gov/ct2/results?cond=&term=Medpace&cntry=&state=&city=&dist=

I need to click "Show/Hide Columns" then select "Study Type", "Phase", "Sponsor/Collaborators", "Number Enrolled", "NCT Number", "Study Start", "Study Completion" and "Last Update Posted".

The class for the "Show/Hide Columns" button: .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
1
  • 2
    Why don't just use a link for xml or csv data download? Commented Jun 19, 2019 at 20:15

1 Answer 1

2

Have your additional desired options in an array then loop all buttons checking if the innerText for button is in the array. If it is then set the class name so the button is active

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.