问题
I know the login credentials are 100% accurate but I receive the message "wrong username or password"?
Sorry I cant give you the login details but I was hoping some one could take a look at the login page here https://grp.controlant.com/user/login. I have used inspect element to try and understand what is happening but its all a bit of a puzzle to me.
please could someone give me some pointers so I can login in successfully!
I have studied the login page HTML source code and I think it may have something to do with this line:-
<p class="ng-cloak" ng-if="loginError">Wrong username or password</p>
<input name="__RequestVerificationToken" type="hidden" value="O9qaspsZWkSDJc2z8hFkGTqT8XAQ2xAY2QcHMpK1w6v_VKw3T-zQ1H8VpoxARDCnGi-Ed2saqXJjwkE7Hsh4RY2kp6pC_AAATZW7vqFynKw1" />
Can anyone confirm if I am going in the right direction?
Below is the code I am using.
Thanks!
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub test1()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
MyURL = "https://grp.controlant.com/user/login"
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 = "my_username" 'Enter your email id here
HTMLDoc.all.Password.Value = "my_password" 'Enter your password here
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("button")
If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
Next
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
回答1:
If you type username and password manually and submit the form then request form data is e. g.
grant_type=password&username=qwert12345&password=asdfg67890&client_id=FMUI&client_secret=thisisnotverysecretiknow
If you assign the values to inputs programmatically, then request form data is
grant_type=password&username=undefined&password=undefined&client_id=FMUI&client_secret=thisisnotverysecretiknow
As you can see credentials are undefined. Also take a look at <form> and <input> nodes content in developer tools before submit. When the page just loaded form class property is ng-pristine ng-valid, inputs class property is ng-pristine ng-untouched ng-valid ng-empty. After you type username and password manually the properties change to ng-valid ng-dirty ng-valid-parse and ng-valid ng-not-empty ng-dirty ng-valid-parse ng-touched.
So, it's not enough just to assign values to inputs. I tried even to assign values programmatically and then to change class properties manually by editing DOM nodes but to no avail. The only way I've found is to fire generic change event for inputs.
Option Explicit
Sub Login()
Dim objEvent, objNodeUsername, objNodePassword, objNodeSubmit
With CreateObject("InternetExplorer.Application")
.Visible = True
' open the page
.Navigate "https://grp.controlant.com/user/login"
' wait until IE and the page are ready
Do While .Busy Or Not .readyState = 4: DoEvents: Loop
' wait until the DOM is ready
Do Until .document.readyState = "complete": DoEvents: Loop
' wait until the username input appears
Do While TypeName(.document.getElementById("username")) = "Null": DoEvents: Loop
' support for dispatchEvent was added in IE9
' create event object
Set objEvent = .document.createEvent("HTMLEvents")
' setup event
objEvent.initEvent "change", True, True
' get username input and assign value
Set objNodeUsername = .document.getElementById("username")
objNodeUsername.Value = "qwert12345"
' send event to the input node
objNodeUsername.dispatchEvent objEvent
' get password input and assign value
Set objNodePassword = .document.getElementById("password")
objNodePassword.Value = "asdfg67890"
' send event to the input node
objNodePassword.dispatchEvent objEvent
' get button and click
Set objNodeSubmit = .document.getElementsByTagName("button")(0)
objNodeSubmit.Click
End With
End Sub
回答2:
You aren't actually supplying the username and password to the fields. To do so replace
HTMLDoc.all.UserName.Value = "my_username" 'Enter your email id here
HTMLDoc.all.Password.Value = "my_password" 'Enter your password here
With the following (I looked at the HTML source to find the tags' ids
HTMLDoc.GetElementById("username").Value = "my_username" 'Enter your email id here
HTMLDoc.GetElementById("password").Value = "my_password" 'Enter your password here
来源:https://stackoverflow.com/questions/37367961/incorrect-username-or-password-when-using-auto-login-macro-in-excel