VBA for clicking a button in IE

snowboy001

Board Regular
Joined
Dec 5, 2009
Messages
100
I have figured out how to put my User Name and Password into the webpage, but I can not figure out how to click on the Logon button. Can anyone help out with this?

Code:
Sub Login()
Dim appIE As InternetExplorer
Dim sURL As String
Dim UserN As Variant, Pw As Variant
Dim Element As HTMLButtonElement
Dim btnInput As MSHTML.HTMLInputElement
Dim ElementCol As MSHTML.IHTMLElementCollection
Application.ScreenUpdating = False
Set appIE = New InternetExplorer
sURL = "[URL="http://172.23.5.53:8081/wells/Login.jsp"]my url[/URL]"
With appIE
    .navigate sURL
    .Visible = True
End With
Do While appIE.Busy
Loop
Set UserN = appIE.document.getElementsByName("User")
If Not UserN Is Nothing Then
    UserN(0).Value = "my username"
End If
Set Pw = appIE.document.getElementsByName("password")
If Not Pw Is Nothing Then
    Pw(0).Value = "my password"
End If
Set ElementCol = appIE.document.getElementsByTagName[COLOR=black]("[COLOR=#ff0000]I need this value[/COLOR]")[/COLOR]
For Each btnInput In ElementCol
    If btnInput.Value = "[COLOR=#ff0000]I need this value[/COLOR]" Then
        btnInput.Click
        Exit For
    End If
Next btnInput
Do While appIE.Busy
Loop
Application.ScreenUpdating = True
Set appIE = Nothing
End Sub

The HTML source coding for the button is:

Code:
[COLOR=#0000ff]<[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]input[/COLOR][COLOR=#0000ff] [/COLOR][COLOR=#ff0000]type[/COLOR][COLOR=#0000ff]="hidden" [/COLOR][COLOR=#ff0000]value[/COLOR][COLOR=#0000ff]="login" [/COLOR][COLOR=#ff0000]name[/COLOR][COLOR=#0000ff]="jboEvent">[/COLOR][COLOR=#0000ff]</[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]a[/COLOR][COLOR=#0000ff] [/COLOR][COLOR=#ff0000]href[/COLOR][COLOR=#0000ff]="#" [/COLOR][COLOR=#ff0000]*******[/COLOR][COLOR=#0000ff]="submitForm('form1',1,{'jboEvent':'login'});return false">[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]img[/COLOR][COLOR=#0000ff] [/COLOR][COLOR=#ff0000]src[/COLOR][COLOR=#0000ff]="/abc/cabo/images/cache/en/b-login.gif" [/COLOR][COLOR=#ff0000]alt[/COLOR][COLOR=#0000ff]="Logon" [/COLOR][COLOR=#ff0000]border[/COLOR][COLOR=#0000ff]="0" [/COLOR][COLOR=#ff0000]align[/COLOR][COLOR=#0000ff]="absmiddle" [/COLOR][COLOR=#ff0000]width[/COLOR][COLOR=#0000ff]="52" [/COLOR][COLOR=#ff0000]height[/COLOR][COLOR=#0000ff]="21">[/COLOR][COLOR=#0000ff]</[/COLOR][COLOR=#800000]a[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]</[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]</[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR]
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
You could click the button but it's a better idea to submit the form the controls are on.

The reason for that is that clicking the button might not actually carry out the same actions as submitting the form.

If you do want to click the button just use the same method you use for the other controls to get a reference to it and then code it's click method.

I see you are sort of doing that but have changed to using GetElementsByTagName.

As far as I can see the button does have a name - jboEvent.

If you do want to try and use GetEle...ByTagName then you would use INPUT and the collection returned would include every input element on the page.:)

Hope that makes sense.
 
Upvote 0
What coding would I use to submit the form? This is my first time playing around with IE through VBA :)
 
Upvote 0
Eh, submit?
Code:
Set frm = IE.Document.GetElementByID("logonform")
 
frm.Submit
That should work but it's not guaranteed, it really depends how the page has been designed.

Oh, obviously change the GetElement... bit if you can't find the ID for the form.

You could use this.
Code:
Set frms = IE.Document.GetElementsByTagName("FORM")
That should return a collection of the forms on the page.

If you are lucky the one you want will be the only one, if it isn't then you'll need to use some method to get the other one.

I normally guess.:)

Well, educated guess I suppose - you can put a watch on the variable frms and check examine what's in it as the code runs.
 
Upvote 0
Figured it out!

I replaced:
Code:
For Each btnInput In ElementCol
    If btnInput.Value = "[COLOR=#ff0000]I need this value[/COLOR]" Then
        btnInput.Click
        Exit For
    End If
Next btnInput



With:
Code:
With appIE
    .document.forms(0).submit
    End With
 
Upvote 0

Forum statistics

Threads
1,215,779
Messages
6,126,846
Members
449,343
Latest member
DEWS2031

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