Excel VBA .click事件不适用于 tag

问题描述 投票:0回答:1

我正在尝试自动登录网站以获取帐户余额。

登录按钮上的.click事件不起作用。我觉得我缺少一些简单的东西。

    Sub test()

    Set IE = CreateObject("InternetExplorer.Application")
    my_url = "https://www.synchronycredit.com/eSecurity/Login/login.action?clientId=google&accountType=plcc&langId=en"

    With IE
        .Visible = True
        .navigate my_url
        .Top = 50
        .Left = 530
        .Height = 400
        .Width = 400

    Do Until Not IE.Busy And IE.readyState = 4
        DoEvents
    Loop

    End With


    IE.document.getElementById("loginUserID").Value = "userNameHere"
    IE.document.getElementById("loginPassword").Value = "passWordHere"


    IE.document.getElementByID("secLoginBtn").Click

    Do Until Not IE.Busy And IE.readyState = 4
        DoEvents
    Loop
End Sub

这是网站上的代码:

<a tabindex="0" title="Login to your account" id="secLoginBtn" role="button" style="cursor: pointer;" data-reason="login:start:submit" onclick="javascript:setRememberMeFlag('rememUID');" data-type="button" class="ADAbtn btn secLoginBtn secureLockImg ensightenmEvent">

                                        <img class="pull-left" src="/essimages/cs/groups/ess_webasset/generic/@common/en/brandimages/044828.png" alt="">
                                    Secure Login</a>
excel vba href
1个回答
0
投票

首先:在[[every VBA模块中,始终将Option Explicit用作第一行。

要解决您的问题,您首先需要触发输入字段的按键事件。输入值后,您可以单击提交按钮。

第一行:

Option Explicit

Main makro:

Sub test() Dim browser As Object Dim url As String Dim nodeUserName As Object Dim nodePassWord As Object Dim nodeSubmitButton As Object Dim domDoc As Object Dim userName As String Dim passWord As String userName = "Your Name" passWord = "YourPassword" Set browser = CreateObject("InternetExplorer.Application") url = "https://www.synchronycredit.com/eSecurity/Login/login.action?clientId=google&accountType=plcc&langId=en" 'Initialize Internet Explorer, set visibility, 'call URL and wait until page is fully loaded Set browser = CreateObject("internetexplorer.application") browser.Visible = True browser.navigate url browser.Top = 50 browser.Left = 530 browser.Height = 400 browser.Width = 400 Do Until browser.readyState = 4: DoEvents: Loop Set domDoc = browser.document Set nodeUserName = domDoc.getElementByID("loginUserID") Call TriggerEvent(domDoc, nodeUserName, "onKeypress") nodeUserName.Value = userName Set nodePassWord = domDoc.getElementByID("loginPassword") Call TriggerEvent(domDoc, nodePassWord, "onKeypress") nodePassWord.Value = passWord Set nodeSubmitButton = domDoc.getElementByID("secLoginBtn") nodeSubmitButton.Click End Sub

用于触发html事件的子例程:

Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String) Dim theEvent As Object htmlElementWithEvent.Focus Set theEvent = htmlDocument.createEvent("HTMLEvents") theEvent.initEvent eventType, True, False htmlElementWithEvent.dispatchEvent theEvent End Sub
© www.soinside.com 2019 - 2024. All rights reserved.