如何通过VBA输入实际文本(如真实的键盘笔划)



我试图创建一个自动登录该网站的脚本。我已经设法在输入框中输入了它,但点击登录按钮后,它似乎没有读取我的输入。一个错误一直说电子邮件地址和密码是必需的。但如果我手动输入凭据,那没关系,我可以登录,否则会提示另一条错误消息输入不正确的凭据。

我认为网站仍然将其作为占位符读取,而不是实际的文本值?

Sub Sample()
Dim sUrl As String: sUrl = "https://www.investagrams.com/Stock/RealTimeMonitoring"
Dim ie As Object
Dim objNode As Object
'Dim inputElement As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate sUrl
Do While ie.readyState <> 4: DoEvents: Loop
Set inputCollection = ie.document.getElementsByTagName("input")
Set colNodes = ie.document.getElementsByClassName("btn btn-lg btn-block investa-login--btn-login btn-investa__medium--blue btn-investa__medium")
For Each inputElement In inputCollection
If inputElement.getAttribute("data-ng-model") = "LoginRequest.Username" Then
inputElement.Click
inputElement.Value = "test98@yahoo.com"
End If
If inputElement.getAttribute("data-ng-model") = "LoginRequest.Password" Then
inputElement.Click
inputElement.Value = "test"
End If
Next inputElement
For Each objNode In colNodes
If objNode.getAttribute("data-ng-click") = "authenticateUser()" Then
objNode.Click
End If
Next objNode

End Sub
Email Input Box
<input type="text" class="form-control ng-valid-maxlength ng-not-empty ng-dirty ng-valid-parse ng-valid ng-valid-required ng-touched" data-ng-disabled="IsLoading" 
data-ng-model="LoginRequest.Username" data-on-enter-keydown-directive="authenticateUser()" 
required="required" placeholder="Email Address" maxlength="40" style="">
Password Input Box
<input type="password" class="form-control ng-pristine ng-empty ng-invalid ng-invalid-required ng-valid-maxlength 
ng-touched" placeholder="Password" data-ng-disabled="IsLoading" 
data-ng-model="LoginRequest.Password" data-on-enter-keydown-directive="authenticateUser()" required="required" maxlength="40" style="">

由于此页面似乎使用jQuery,因此您可以使用它来模拟页面上的事件,这比使用当前方法要容易得多。

Sub Sample()
With CreateObject("InternetExplorer.Application")
.Visible = True
.navigate "https://www.investagrams.com/Stock/RealTimeMonitoring"
Do While .readyState <> 4: DoEvents: Loop
.document.parentWindow.execScript "$('form input:eq(0)').val('MyUserName').change();"
.document.parentWindow.execScript "$('form input:eq(1)').val('MyPassword').change();"
.document.parentWindow.execScript "$('form button').click();"
Stop
End With
End Sub

最新更新