Home » excel » excel – Find form ID on a website and input value using VBA

excel – Find form ID on a website and input value using VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

I would require a bit of help finding a form id on a public website (http://www.medicines.ie/). They updated the site and the previous id no longer works since now there is no Id to be found. What I am trying to do is open this site with VBA, input the value from a specific cell in excel to a form (textbox) on this website and press the search button. I am using the code below:

Sub Medicinesie()
Dim IE As Object
Set IE = CreateObject("INTERNETEXPLORER.APPLICATION")
IE.navigate "http://www.medicines.ie/"
IE.Visible = True

Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE

    IE.Document.getElementById("input").Value = Range("spc")  '<---- spc is the name of the cell I am referencing 
    IE.Document.forms(0).submit

Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE

End Sub
How to&Answers:

It looks like you could take advantage of the URL construction at this website. URL is constructed:
http://www.medicines.ie/medicines?page=1&per-page=25&query= + anything you would like to search in this database.

Sub MedicineS()
Dim IE As Object
Set IE = CreateObject("INTERNETEXPLORER.APPLICATION")
Dim URL As String

URL = "http://www.medicines.ie/medicines?page=1&per-page=25&query=" & _ 
Range("spc")
IE.Visible = True
IE.navigate URL

Do While IE.readyState <> READYSTATE_COMPLETE
Loop

End Sub

However if you still prefer to use your own way keep in mind that input you are looking for is 4th in the code so:
IE.Document.getElementByTagName(“input”)(3).Value
and the button is second so:
IE.Document.getElementByTagName(“button”)(1).Click

Answer:

With selenium vba wrapper installed and adding tools > reference > selenium type library

Option Explicit
Public Sub test()
    Dim d As WebDriver
    Set d = New ChromeDriver '<== can change to internet explorer driver

    With d
        .Start "Chrome"
        .Get "http://www.medicines.ie/"
        .FindElementByCss("input.search__input").SendKeys "Aspirin" '<== Range("spc")
        .FindElementByTag("form").Submit
        Stop
        '.Quit
    End With
End Sub

Example run:

Run