首页 > 解决方案 > 如何通过 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="">

标签: excelvba

解决方案


由于此页面似乎使用 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

推荐阅读