Login to a website using a macro and then click a button

jeremypyle

Board Regular
Joined
May 30, 2011
Messages
174
Hi

I am trying to login to Home

This is my code so far. Please note, username and password is only example. I have worked out how to get it to fill in the username and password I think. But not sure how to make it click the button

I think the error with my code is:
Set Button = .getElementsByClassName("btn btn-primary button-shadow login-button")

But I'm not exactly sure how to fix this


Sub FollowWebsiteLink()

Dim ie As Object
Dim htmldoc As HTMLDocument
Dim UserName As String, Password As String
UserName = "jeremy"
Password = "asdf1234"




Application.ScreenUpdating = False

Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "Home"

Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Loading website..."
DoEvents
Loop


newwebsite = (ie.document.getElementsByClassName("my-ir hidden-xs btn btn-green myir-popup__desktop-trigger"))
Set htmldoc = ie.document

ie.navigate newwebsite

Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Loading website..."
DoEvents
Loop

With htmldoc

Dim oLogin As Object, oPassword As Object
Set oLogin = .getElementsByTagName("input")(1)
Set oPassword = .getElementsByTagName("input")(2)
Set Button = .getElementsByClassName("btn btn-primary button-shadow login-button")


oLogin.Value = UserName
oPassword.Value = Password
Button.Click

End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
You were very close! I got the code below to work for me... save for the fact that it's the wrong username and password (obviously), that you didn't put a URL for IE to navigate to (your's just says Home) and importantly, that you didn't specify which button with the class you wanted to click... the method getElementsByClassName says Elements on purpose - it will return a collection of elements, from which you need to select which one you want. It took me a long time to work this out (a lightbulb moment, for me). Even if there is only one element on the whole page with that class name, you would then need to designate which one the same way you did with the preceding two lines of code (i.e., (1) and (2)) or as you did earlier in the code with the last button, wrap the whole thing in parentheses:
VBA Code:
newwebsite = (ie.Document.getElementsByClassName("my-ir hidden-xs btn btn-green myir-popup__desktop-trigger"))
Hope that helps.

VBA Code:
Sub FollowWebsiteLink()

Dim ie As Object
Dim htmldoc As HTMLDocument
Dim UserName As String, Password As String
UserName = "jeremy"
Password = "asdf1234"

Application.ScreenUpdating = False

Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "https://ird.govt.nz/"

Do While ie.ReadyState <> READYSTATE_COMPLETE
Application.StatusBar = "Loading website..."
DoEvents
Loop

newwebsite = (ie.Document.getElementsByClassName("my-ir hidden-xs btn btn-green myir-popup__desktop-trigger"))
Set htmldoc = ie.Document

ie.Navigate newwebsite

Do While ie.ReadyState <> READYSTATE_COMPLETE
Application.StatusBar = "Loading website..."
DoEvents
Loop

With htmldoc

Dim oLogin As Object, oPassword As Object
Set oLogin = .getElementsByTagName("input")(1)
Set oPassword = .getElementsByTagName("input")(2)
Set Button = (.getElementsByClassName("btn btn-primary button-shadow login-button"))

End With
oLogin.value = UserName
oPassword.value = Password
Button.Click

End Sub
 
Upvote 0
Oh, and you were missing an End With, which I guess was a copy/paste mistake.
 
Upvote 0
Thanks so much for this Dan. Sorry I did have website address correct at first but when I copied and pasted something didn't work properly.

I had:
Set Button = .getElementsByClassName("btn btn-primary button-shadow login-button")

But you have:
Set Button = (.getElementsByClassName("btn btn-primary button-shadow login-button"))

I still don't understand why these extra brackets are necessary. However yours works!


Thanks so much

Jeremy
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

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