VBA Code to login into a Website

bunburyst

New Member
Joined
Apr 18, 2018
Messages
24
Hey guys!

I need to have my VBA macro enter the user name and pasword to login a website.
I have read a number of posts related to this topic, tried a number of different methods and still can't get it to work.

Code:
Dim HTMLDoc As HTMLDocumentDim MyBrowser As InternetExplorer
 Sub RepsolLogin()






 Dim MyHTML_Element As IHTMLElement
 Dim MyURL As String
 MyURL = "https://login.repsol.com/es/Landing/AuthnPage?returnUrl=https://www.repsol.com/es_es/"
 Set MyBrowser = New InternetExplorer
 MyBrowser.Silent = True
 MyBrowser.navigate MyURL
 MyBrowser.Visible = True


 Do
 Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
 Set HTMLDoc = MyBrowser.document
 HTMLDoc.all.UserName.Value = "xxx@xxx.com" 'Enter your email id here
 HTMLDoc.all.Password.Value = "xxxx" 'Enter your password here
 For Each MyHTML_Element In HTMLDoc.getElementById("Iniciar sesión")
 If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
 Next


 End Sub


Can anyone have a look an tell me how I must have the macro?
Any help is appreciated. Thanks.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
It is loading the form asynchronously, so you need to make sure if the form element is loaded first. After doing that, you can access its input and submit elements.

Please try the following.

Code:
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer


Sub RepsolLogin()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
Dim form As HTMLFormElement
    MyURL = "https://login.repsol.com/es/Landing/AuthnPage?returnUrl=https://www.repsol.com/es_es/"
    Set MyBrowser = New InternetExplorer
    MyBrowser.Silent = True
    MyBrowser.navigate MyURL
    MyBrowser.Visible = True
    Do
        DoEvents
    Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE
 
    Set HTMLDoc = MyBrowser.document
 
    Do While HTMLDoc.getElementById("gigya-login-form") Is Nothing
        DoEvents
    Loop
 
    Set form = HTMLDoc.getElementById("gigya-login-form")
 
    form.all.UserName.Value = "xxx@xxx.com" 'Enter your email id here
    form.all.Password.Value = "password" 'Enter your password here


    form.submit
End Sub

Hope it helps.

Suat
 
Upvote 0
Ey my friend, I thought everything worked...

the macro is working for the user name and password but login not working..
 
Upvote 0
Ey my friend, I thought everything worked...

the macro is working for the user name and password but login not working..

Make sense, submit action must be checked with JavaScript, so native form.submit didn't help I guess.

Ok, instead of form.submit, let's try following:

Code:
form.getElementsByClassName("gigya-input-submit")(0).click

There is no good attribute to use on the submit (input) element, the easiest one looks to be this. I don't have Windows anywhere near, so I can't check it at the moment. So please let me know. If it doesn't work then we try something else.

Hope it helps.

Suat
 
Upvote 0
Solution

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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