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!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
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.
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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:
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,603
Members
449,038
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top