excel - 如何通过 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
推荐阅读
- javascript - 函数被导出为空白对象?
- graph - 从 pandas 读取的 Networkx 为同一源创建 2 个节点
- git - git stash 直接应用到暂存区
- ios - swift5中更新和删除钥匙串的问题
- apache - 在 Windows 上使用 XAMPP 的 android webview 上的 net::ERR_CONNECTION_RESET 错误
- excel - 如何使用excel VBA将excel选项卡的格式复制并粘贴到除选定工作表之外的其他选项卡
- c - 在 C 中制作我的邻接矩阵时出了什么问题?
- grpc-go - http2Server HandleStreams 失败
- javascript - 遍历 2 个包含对象的数组并使用第一个数组键返回第二个数组的值
- node.js - Elastic APM Node.js - 不监控 HTTP,只监控自定义事务