Home » excel » excel – How to click a <button> in VBA

excel – How to click a <button> in VBA

Posted by: admin May 14, 2020 Leave a comment


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
            .getElementsByClassName("dt-button buttons-collection buttons-colvis").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

        ' 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)

    ' 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
How to&Answers:

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
        End With
    End With
End Sub