[英]incorrect Username or password when using auto login macro in excel
I know the login credentials are 100% accurate but I receive the message "wrong username or password"? 我知道登录凭据是100%准确但我收到消息“用户名或密码错误”?
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 . 抱歉,我无法提供登录详细信息,但我希望有人可以在这里查看登录页面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.
我已经使用了inspect元素来尝试理解发生了什么,但这对我来说是一个难题。
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:- 我已经研究了登录页面HTML源代码,我认为它可能与这一行有关: -
<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
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 使用以下内容(我查看了HTML源代码以查找标签的ID
HTMLDoc.GetElementById("username").Value = "my_username" 'Enter your email id here
HTMLDoc.GetElementById("password").Value = "my_password" 'Enter your password here
If you type username and password manually and submit the form then request form data is eg 如果您手动输入用户名和密码并提交表单,则请求表单数据为例如
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
. 如您所见,凭据
undefined
。 Also take a look at <form>
and <input>
nodes content in developer tools before submit. 在提交之前,还要查看开发人员工具中的
<form>
和<input>
节点内容。 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
. 当刚刚加载的表格类属性为
ng-pristine ng-valid
,输入类属性为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
. 手动输入用户名和密码后,属性将更改为
ng-valid ng-dirty ng-valid-parse
和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.
我甚至尝试以编程方式分配值,然后通过编辑DOM节点手动更改类属性但无济于事。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.