使用VBA登录后是否可以单击网站上的按钮?

时间:2019-06-13 18:34:13

标签: html excel vba button

这是我编写的代码。它基本上是在第1部分中填充用户名和密码字段以登录所需的网站。之后,在第2部分中,实际上是单击“登录”按钮。 但是,在执行了第1部分和第2部分之后,第3部分应单击一个名为“投资组合管理”的按钮。这是“投资组合管理”按钮的html代码。

但是,我的代码似乎有问题。它实际上在运行第1部分和第2部分,因此实际上是在登录网站,但是此后什么也没有发生。从不单击“资产组合管理”按钮。

您知道我该如何修复代码,使其首先使用我的凭据登录到网站,然后单击“投资组合管理”按钮? 谢谢:)

Option Explicit

Public Sub Press_Button()

Dim objIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll)
Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library
Dim htmlInput As MSHTML.HTMLInputElement
Dim htmlColl As MSHTML.IHTMLElementCollection
Dim the_input_elements As MSHTML.IHTMLElementCollection
Dim input_element As MSHTML.HTMLInputElement
Dim IeDoc As MSHTML.HTMLDocument

Set objIE = New SHDocVw.InternetExplorer

With objIE
    .Navigate "https://www.ndexsystems.com/fengine/fullservice/en/kerrfinancialsalogin.go?fromLogoff=true" ' Main page

    .Visible = 1
    Do While .readyState <> 4: DoEvents: Loop
    Application.Wait (Now + TimeValue("0:00:02"))

    'PART 1: set user name and password
    Set htmlDoc = .document
    Set htmlColl = htmlDoc.getElementsByTagName("INPUT")
    Do While htmlDoc.readyState <> "complete": DoEvents: Loop
    For Each htmlInput In htmlColl
       If htmlInput.Name = "textbox_password" Then
            htmlInput.Value = "***"
        Else
            If htmlInput.Name = "textbox_id" Then
                htmlInput.Value = "***"
            End If
        End If
    Next htmlInput

    'PART 2: click login
    Set htmlDoc = .document
    Set htmlColl = htmlDoc.getElementsByTagName("input")
    Do While htmlDoc.readyState <> "complete": DoEvents: Loop
    For Each htmlInput In htmlColl
        If Trim(htmlInput.Type) = "submit" Then
            htmlInput.Click
            Exit For
        End If
   Next htmlInput

   'PART 3: Clicks on portfolio management
   Do While objIE.Busy: DoEvents: Loop
   Do Until objIE.readyState = READYSTATE_COMPLETE: DoEvents: Loop
            Set IeDoc = objIE.document
            Set the_input_elements = IeDoc.getElementsByClassName("big_button")
            For Each input_element In the_input_elements
                If input_element.href = "javascript:changePageToFrontdoor(false);" Then
                    input_element.Click
                    Exit For
                End If
            Next input_element
End With

End Sub

0 个答案:

没有答案