Clicking a button via IE automation VBA

vba_newbie2013

New Member
Joined
Jun 18, 2013
Messages
29
Hey, i'm trying to click a submit/go button on a webpage and i cant seem to get it to work. Everything i've worked with so far seems to have an ID that works well with getElementById call, but with this button i cant seem to find anything that will reference to it and click. I've tried the following with no success:

getElementsByTagName("").Click/submit
getElementsByClassName("").Click/submit
getElementsByName("").Click /submit
getElementsById("").Click/submit

Here's the HTML code from the webpage i think what i need is located in, also following that is the actual webpage.


Code:
[COLOR=#800000]div[/COLOR][COLOR=#ff0000]class[/COLOR][COLOR=#0000ff]="go-btn-box">[/COLOR]
[COLOR=#0000ff]<[/COLOR][COLOR=#800000]input[/COLOR][COLOR=#ff0000]class[/COLOR][COLOR=#0000ff]="btn go-btn" [/COLOR][COLOR=#ff0000]alt[/COLOR][COLOR=#0000ff]="Get Individual & Family Health Insurance Quotes" [/COLOR][COLOR=#ff0000]title[/COLOR][COLOR=#0000ff]="Get Individual & Family Health Insurance Quotes" [/COLOR][COLOR=#ff0000]name[/COLOR][COLOR=#0000ff]="method:submit" [/COLOR][COLOR=#ff0000]type[/COLOR][COLOR=#0000ff]="image" [/COLOR][COLOR=#ff0000]*******[/COLOR][COLOR=#0000ff]="return shoppingCart.checkCensus($('#mainForm'), 'IFP', 'submit');" [/COLOR][COLOR=#ff0000]width[/COLOR][COLOR=#0000ff]="145" [/COLOR][COLOR=#ff0000]height[/COLOR][COLOR=#0000ff]="44" [/COLOR][COLOR=#ff0000]src[/COLOR][COLOR=#0000ff]="http://static.ehealthinsurance.com/ehealthinsurance/images_new/buttons/wide-census-go.gif" />[/COLOR]
[COLOR=#0000ff]</< font>[COLOR=#800000]div[/COLOR][COLOR=#0000ff]>[/COLOR]
[/COLOR]


Website: Individual and Family Health Insurance

Thanks for any help you can provide!
 

Some videos you may like

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,047
Office Version
  1. 365
Platform
  1. Windows
You haven't posted the code you are using or said how what you've tried hasn't work.

If the aim of the button click is to submit a form, and that form is the only one on the page, then you might want to try IE.document.forms(0).submit.
 

vba_newbie2013

New Member
Joined
Jun 18, 2013
Messages
29
Norie i think you might be on to something with the forms, but from testing what you suggested it presses the search button. I tried IE.document.form(1).submit to no avail (seems to just reload the page oddly.) As for the code i'm using it is somewhat irrevelant because everything runs correctly without this snippet:

Code:
Set goBtn = IE.document.getElementById("btn go-btn")
goBtn.Click

To which it is throwing the typical 'Object doesn't support this property or method.' error. I've tried this and several other cases. (IE.document.getElementsByClassName("btn go-btn), goBtn.Submit, etc.)
Thanks for responding by the way :). I was beginning to lose hope. Next time i'll try to provide a little more context.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,047
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

The buttons and inputs are contained within the form, so you should be able to access them through the form.

Without seeing your code something like this might work.

Code:
Set btnGo = IE.document.forms(0).all("method:submit") ' method:submit is the button's name

btnGo.click
 

vba_newbie2013

New Member
Joined
Jun 18, 2013
Messages
29
Thanks for the quick reply. Hopefully i didnt come off as rude when i said you didnt need my code. Here it is.

Code:
Dim IE As InternetExplorer
Dim myZip As Long
Dim siteZip As Object
Dim URL As String
Dim gender As Object
Dim btnGo As Object

Sub Macro_1()
myZip = "12345"
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "[URL="http://www.ehealthinsurance.com/individual-family-health-insurance"]Individual and Family Health Insurance[/URL]"
IE.navigate (URL)

Do
DoEvents
Loop Until IE.readyState = 4

Set siteZip = IE.document.getElementById("census_zipCode")
siteZip.Value = myZip

Set gender = IE.document.getElementById("census_primary_gender")
gender.Value = "MALE"

Set btnGo = IE.document.forms(0).all("method:submit") ' method:submit is the button's name
btnGo.Click

End Sub

Still not working, but this time i'm getting a 'Object variable or With variable not set.' error. I just copied your code and plopped it in with an added Dim. Any help?
 

pman

Board Regular
Joined
Dec 19, 2011
Messages
114

ADVERTISEMENT

something like this:

Code:
Set objInputs = IE.Document.getElementsByTagName("input")

For Each ele In objInputs
    If ele.Title Like "Get Individual & Family Health Insurance Quotes" Then
        ele.Click
    End If
Next
 

pman

Board Regular
Joined
Dec 19, 2011
Messages
114
so putting that in with your code:

Code:
Dim IE As InternetExplorer
Dim myZip As Long
Dim siteZip As Object
Dim URL As String
Dim gender As Object
Dim btnGo As Object

Sub Macro_1()
myZip = "12345"
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "Individual and Family Health Insurance"
IE.Navigate (URL)

Do
DoEvents
Loop Until IE.ReadyState = 4

Set siteZip = IE.Document.getElementById("census_zipCode")
siteZip.Value = myZip

Set gender = IE.Document.getElementById("census_primary_gender")
gender.Value = "MALE"

Set objInputs = IE.Document.getElementsByTagName("input")

For Each ele In objInputs
    If ele.Title Like "Get Individual & Family Health Insurance Quotes" Then
        ele.Click
    End If
Next

End Sub
 

vba_newbie2013

New Member
Joined
Jun 18, 2013
Messages
29
pman your awesome. Worked brilliantly. couldnt be here without Norie first being here though so you all are both awesome :p. Do have a quesiton though. I think i understand how it works but if you could explain it a little more so i can use it in the future that'd be cool, no worries if not. Once again thanks!

Also i'm not sure if this is allowed but if one of you knows the answer to my other question that'd be even more awesome :biggrin::
http://www.mrexcel.com/forum/excel-...x-ie-use-excel-visual-basic-applications.html
 
Last edited:

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,047
Office Version
  1. 365
Platform
  1. Windows
There's more than one form on the site, the one you want is the second form, forms(1).

Another problem is there are two elements called 'method:submit' on the form, the first one is the button.
Code:
Sub GetApABFileName()
Dim IE As InternetExplorer
Dim myZip As Long
Dim siteZip As Object
Dim URL As String
Dim gender As Object
Dim btnGo As Object
Dim frm As Object


    myZip = "12345"
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    URL = "http://www.ehealthinsurance.com/individual-family-health-insurance"
    IE.navigate (URL)

    Do
        DoEvents
    Loop Until IE.readyState = 4

    Set siteZip = IE.document.getElementById("census_zipCode")
    siteZip.Value = myZip

    Set gender = IE.document.getElementById("census_primary_gender")
    gender.Value = "MALE"

    Set frm = IE.document.forms("main")
    
    Set btnGo = frm.all("method:submit")(0)    ' method:submit is the button's name
    btnGo.Click

End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,118,121
Messages
5,570,304
Members
412,318
Latest member
angoeyuan
Top