如何使用VBA单击Internet Explorer中新打开的选项卡中存在的按钮?

时间:2019-06-17 13:59:31

标签: excel vba internet-explorer

这是我编写的代码。

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
Dim IeDoc2 As MSHTML.HTMLDocument
Dim input_element2 As MSHTML.HTMLInputElement
Dim the_input_elements2 As MSHTML.IHTMLElementCollection

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 button 
   Do While .Busy: DoEvents: Loop
   Do Until .readyState = READYSTATE_COMPLETE: DoEvents: Loop
            Set IeDoc = .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

    'PART 4: Clicks on the 'Advanced search' button
    Do While .Busy: DoEvents: Loop
    Do Until .readyState = READYSTATE_COMPLETE: DoEvents: Loop
    Set IeDoc2 = .document
    Set the_input_elements2 = IeDoc2.getElementsByClassName("parent-item")
    For Each input_element2 In the_input_elements2
    If input_element2.href = "javascript:directToSearch()" Then
    input_element2.Click
    Exit For
    End If
    Next input_element2



End With

End Sub

第1部分,第2部分和第3部分工作正常。当我运行此宏时,它实际上是使用我的凭据登录的网站。在第3部分中,它还单击了名为“ Porfolio管理”的按钮。 但是,通过单击“项目组合管理”按钮,将打开一个新标签,其中包含同一网站的另一个页面。 在这个新打开的页面上,有一个我想单击的按钮,称为“高级搜索”。这是按钮的HTML代码。 enter image description here

第4部分不适用于此代码。它没有给我任何错误,只是没有做任何事情。我不知道我的错误在哪里,因为我用与第3部分完全相同的语法编写了第4部分,实际上只有第3部分正在运行并给出正确的结果(单击按钮)。

也许第3部分打开了该网站的新标签这一事实应该暗示我在第4步中没有执行的其他步骤?由于我不再使用同一个标签...

有人可以帮助我找到错误吗?

谢谢:)

1 个答案:

答案 0 :(得分:0)

如果该URL每次都不相同,并且您无法使用该URL,则可以尝试参考以下步骤。

(1)创建Shell应用程序的对象。

(2)比尝试计算IE窗口并循环浏览。

(3)之后,将页面标题与所有选项卡匹配,找到所需的选项卡并将其分配给IE对象,然后您可以尝试与该页面进行交互。

示例:

Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
    On Error Resume Next    ' sometimes more web pages are counted than are open
    my_url = objShell.Windows(x).Document.Location
    my_title = objShell.Windows(x).Document.Title

 'find the desired page
    If my_title Like "Put something from your IE page title here" & "*" Then
        Set ie = objShell.Windows(x)
        Exit For
    Else
    End If
Next

您可以根据需要修改以上代码。

有关更多信息,请参见下面的链接。

VBA-Excel: Get ALL The Opened Internet Explorer (IE) using Microsoft Excel