如何通过VBA输入实际文本(例如真实的键盘按键)?

时间:2019-11-11 08:29:25

标签: 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="">

1 个答案:

答案 0 :(得分:0)

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