简体   繁体   English

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

[英]How is it possible to click on a button present in a newly opened tab in internet explorer using VBA?

Here is the code I wrote. 这是我写的代码。

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

Parts 1, 2 and 3 work perfectly. 第1,2和3部分完美运行。 When I run this macro, it is actually logging in the website with my credentials. 当我运行这个宏时,它实际上是使用我的凭据登录网站。 In part 3, it is also clicking on the button called "Porfolio management". 在第3部分中,它还点击了名为“Porfolio management”的按钮。 However, by clicking on the "portfolio management" button, a new tab is opened with another page of the same website. 但是,通过单击“项目组合管理”按钮,将打开一个新选项卡,其中包含同一网站的另一个页面。 On this newly opened page, there is a button called "Advanced search" that I want to click. 在这个新打开的页面上,有一个名为“高级搜索”的按钮,我想点击它。 Here is the HTML code of the button. 这是按钮的HTML代码。 在此输入图像描述

Part 4 is not working with this code. 第4部分不使用此代码。 It is not giving me any error, it is just not doing anything. 它没有给我任何错误,它只是没有做任何事情。 I don't know where my error is because I wrote part 4 with the exact same syntax as part 3 and it is only part 3 that is actually running and giving the correct result (clicking on the button). 我不知道我的错误在哪里,因为我用与第3部分完全相同的语法编写了第4部分,并且它只是第3部分实际运行并给出了正确的结果(单击按钮)。

Maybe the fact that part 3 opens a new tab of this website should imply an additional step that I didn't do in step 4? 也许第3部分打开这个网站的新标签的事实应该意味着我在第4步没有做的额外步骤? Since I am not working with the same tab anymore... 因为我不再使用相同的标签了...

Can anyone help me with finding the error? 任何人都可以帮我找到错误吗?

Thank you :) 谢谢 :)

If the URL is different every time and you are not available with that URL than you can try to refer steps below. 如果URL每次都不同,并且您不能使用该URL,那么您可以尝试参考以下步骤。

(1) create object of Shell application. (1)创建Shell应用程序的对象。

(2) Than try to count IE windows and loop through it. (2)尝试计算IE窗口并循环通过它。

(3) After that match the page title with all the tabs and find the desired tab and assigned it to IE object and than you can try to interact with that page. (3)之后,将页面标题与所有选项卡匹配,找到所需的选项卡并将其分配给IE对象,然后您可以尝试与该页面进行交互。

Example: 例:

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

You can modify the above code based on your requirement. 您可以根据您的要求修改上述代码。

For more information, you can refer link below. 有关更多信息,请参阅以下链接。

VBA-Excel: Get ALL The Opened Internet Explorer (IE) using Microsoft Excel VBA-Excel:使用Microsoft Excel获取所有打开的Internet Explorer(IE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM