PDA

View Full Version : VBA MACRO Button click using a class instead of ID



pedrosc
04-22-2020, 10:07 AM
Dear Users,

I am trying to run a sub that does the following:

Access this website (https://clinicaltrials.gov/)
Introduces some words in the boxes "Condition or disease" and "other terms"
Clicks in the button "search"


I did everything but point 3. I can't do point three because the button does not have an ID but has a class. I tried to use a class but then I can't run the command .Click I don't know exactly why.

Here it is the code I have been using. When I run the macro I keep getting this error: Object does not support this method (this basically is telling me I can.t use the command .click with this getelement...

PS: if you know any good resource for data scrapping please share with me I would be forever grateful


CODE

Sub pullDatafromweb()
Dim IE As Object
Dim doc As HTMLDocument


'initializing the IE'


Set IE = CreateObject("InternetExplorer.Application")


'allows us to see the browser getting launched'


IE.Visible = True


'telling the object to go to the following object'


IE.Navigate "https://clinicaltrials.gov"


'tells the system to wait until the page is loaded before moving into the next line'


Do While IE.Busy Or IE.ReadyState <> 4
Application.Wait DateAdd("s", 1, Now)


'puts the specificed values/strings in the search boxed'


Loop
Set doc = IE.document
doc.getElementById("home-search-condition-query").Value = "GvHD"
IE.document.getElementById("home-search-other-query").Value = "MSC"
doc.getElementByTagClass("input.ct-searchButton.hp-searchButtonn").Click

End Sub

END CODE

Zack Barresse
04-22-2020, 07:08 PM
Just a precursory glance, everything looks normal, but isn't the class tag for the search button "ct-searchButton hp-searchButton" and not "input.ct-searchButton.hp-searchButtonn"?

Zack Barresse
04-22-2020, 07:19 PM
Tested and worked for me. The line you're looking for is:


doc.getElementsByClassName("ct-searchButton hp-searchButton").Item(0).form.submit

Edit: You may want to look here (https://clinicaltrials.gov/ct2/resources/download#UseURL), they have an API.

pedrosc
04-23-2020, 12:36 AM
You are an ABSOLUTE LEGEND!!!!!

Thank you so much!!!!!!!!

Zack Barresse
04-23-2020, 08:50 AM
lol you're very welcome. I recommend taking a look at their API, it looks pretty nice. Controlling IE will only take you so far, and is not entirely efficient. It can be a good segue for coders because it has a visual you can see, but isn't efficient. There are also the security issues presented with using IE, because, well, it sucks. In the brief reading of their API, it looks like you can easily download multiple studies in a variety of formats.