Log into Website Help ?

razzandy

Active Member
Joined
Jun 26, 2002
Messages
400
Office Version
  1. 2007
Platform
  1. Windows
The VBA code I am using is below and it runs without any errors BUT the button to log in doesn't actually get clicked.


The HTML Code on the website is here:

<form action="/Account/Login" id="loginForm" method="post" role="form"><input name="__RequestVerificationToken" type="hidden" value="BRqw7ES_53Qj1o9MZr-KpltdUyjNvKGaUQ4xjLdv8H7PVWPE82H6eyTanuOn-wXSi64TWDdaVGT3FDfO-dUNI0Gl3H41" /><div class="form-group">
<label class="control-label" for="Email">Email Address</label>
<input autocomplete="off" class="form-control" data-val="true" data-val-email="Please enter a valid email address." data-val-required="The Email Address field is required." id="Email" name="Email" type="text" value="" />
<span class="field-validation-valid text-primary" data-valmsg-for="Email" data-valmsg-replace="true"></span>
</div>
<div class="form-group">
<label class="control-label" for="Password">Password</label>
<input class="form-control" data-val="true" data-val-required="The Password field is required." id="Password" name="Password" placeholder="********" type="password" />
<span class="field-validation-valid text-primary" data-valmsg-for="Password" data-valmsg-replace="true"></span>
</div>
<button type="submit" class="btn btn-primary btn-block"><i class="fa fa-key"></i> Log in</button>
</form>

VBA Code:

Sub LogInToRoyalMail()

cURL = "https://Website here"
Const cUsername = "Username"
Const cPassword = "Password"

Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim UserNameInputBox As HTMLInputElement
Dim PasswordInputBox As HTMLInputElement
Dim SignInButton As HTMLInputButtonElement
Dim HTMLelement As IHTMLElement
Dim qt As QueryTable

Set IE = New InternetExplorer

IE.Visible = True
IE.navigate cURL

'Wait for initial page to load

Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop

Set doc = IE.Document

Set LoginForm = doc.forms(0)

Set UserNameInputBox = LoginForm.elements("Email")
UserNameInputBox.Value = cUsername

Set PasswordInputBox = LoginForm.elements("Password")
PasswordInputBox.Value = cPassword

Set SignInButton = LoginForm.elements("__RequestVerificationToken")
SignInButton.Click

Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop

End Sub

Hope somebody can see where I'm going wrong.

Thanks in advance

:-)
 
Last edited:
Just wondering if all my code could run in Userform with the Webbrowser Object. This way a separate instance of IE wouldn't need to be opened. It would all run within the Userform?

what do you think John?

:-)
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Is there a way to click the 'Save As' option and pass the directory to UIAutomation?
I know that clicking 'Save' works, but I've never managed to get 'Save As' to work using UIAutomation. 'Save As' requires the down arrow next to 'Save' to be clicked first.

Just wondering if all my code could run in Userform with the Webbrowser Object. This way a separate instance of IE wouldn't need to be opened. It would all run within the Userform?

what do you think John?

:-)
The WebBrowser object is basically the same as IE - it has the same properties and methods - so you would still have the same problem of automating the download.
 
Upvote 0
Hi John

Is there a property in UIAutomation where you can say find Application that is invisible? I am asking because it seems UIAutomation will not run if IE is not visible which is what happens when its run as a scheduled task. The other way to experience this is by changing IE.Visaible to False and running the code. Debugging shows the UIAutomation never really gets going.

:-)
 
Upvote 0
My use of the UIAutomation class is very limited, so I don't know if it has something which can find an invisible application. Since UIAutomation emulates manual clicking, etc., I would think it needs the application to be visible.

If I was approaching your task from scratch I would try the XMLhttp method of emulating the browser's requests.
 
Upvote 0
John_w said:
If I was approaching your task from scratch I would try the XMLhttp method of emulating the browser's requests.

Will XMLhttp work with the type of Download link I am using? The file doesn't reside within a fixed URL, its dynamically generated when the download button is clicked.

Cheers

;-)
 
Upvote 0
In theory, yes, as long as the XMLhttp requests emulate exactly what a browser does. I should warn you though that this technique can be time-consuming (a lot of trial and error) to develop and code.
 
Upvote 0
In theory, yes, as long as the XMLhttp requests emulate exactly what a browser does. I should warn you though that this technique can be time-consuming (a lot of trial and error) to develop and code.

Thanks John, I will let you know what I decide ;-)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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