繁体   English   中英

VBA 自动化不适用于新的 IE 选项卡

[英]VBA Automation isn't working on new IE tab

这是我的第一个问题,我是 VBA 新手。

我试图运行一个宏来自动化 IE 上的一些程序。 在不同的窗口上打开时,我的宏运行良好,但是当我尝试在选项卡上运行相同的宏时,它不起作用。 似乎(也许)宏无法识别实际的选项卡。 请问有人可以帮我吗?

Sub Test1()

    Dim IE As Object
    Dim doc As HTMLDocument
    Set IE = CreateObject("InternetExplorer.Application")

    For intRow = 1 To 3

    If intRow = 1 Then

        With IE
            .Visible = True
            .navigate "https://gru.inpi.gov.br/pePI/jsp/patentes/PatenteSearchBasico.jsp"

        Do While IE.Busy Or IE.readyState <> 4
            Application.Wait DateAdd("s", 1, Now)
        Loop

        Set doc = IE.document

            IE.document.getElementById("principal").Children(4). _
                getElementsByTagName("tbody")(0).getElementsByTagName("tr")(5). _
                getElementsByTagName("td")(1).getElementsByTagName("font")(0). _
                getElementsByTagName("input")(0).Value = "intRow"

                Application.Wait DateAdd("s", 2, Now)


        End With
    Else
        With IE
            .Visible = True
            .navigate "https://gru.inpi.gov.br/pePI/jsp/patentes/PatenteSearchBasico.jsp", 2048&

        Do While IE.Busy Or IE.readyState <> 4
            Application.Wait DateAdd("s", 1, Now)
        Loop

            .document.getElementById("principal").Children(4). _
                getElementsByTagName("tbody")(0).getElementsByTagName("tr")(5). _
                getElementsByTagName("td")(1).getElementsByTagName("font")(0). _
                getElementsByTagName("input")(0).Value = "intRow"

                Application.Wait DateAdd("s", 2, Now)

        End With    
    End If
    Next
End Sub

根据您的代码和网站,您似乎要打开多个选项卡(具有相同的URL),然后在文本框中填写一个值。 我们应该注意以下几点:

  1. 如何访问网页元素。
  2. 如何切换选项卡并聚焦到新选项卡。

要访问网页元素,从您的网页资源(使用 F12 开发人员工具检查它),我注意到该表包含 6 行,但最后一行不包含输入元素。 因此,使用您的代码,我找不到输入元素。 我发现输入元素包含类属性。 因此,我建议您可以使用 getElementsByClassName 方法来查找元素。

要切换IE浏览器标签,我们可以遍历打开的标签并比较URL,然后将焦点放在相关标签上。

更详细的步骤,请查看以下示例代码:

 Sub Test1()

    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")

    Dim intRow As Integer
    For intRow = 1 To 3

    If intRow = 1 Then

        With IE
            .Visible = True
            .navigate "https://gru.inpi.gov.br/pePI/jsp/patentes/PatenteSearchBasico.jsp"

        Do While IE.Busy Or IE.readyState <> 4
            Application.Wait DateAdd("s", 1, Now)
        Loop

        Set doc = IE.document

            IE.document.getElementsByClassName("basic")(0).Value = "intRow" & intRow

            Application.Wait DateAdd("s", 3, Now)


        End With
    Else
        With IE
            .Visible = True
            .navigate "https://gru.inpi.gov.br/pePI/jsp/patentes/PatenteSearchBasico.jsp", 2048&

        Do While IE.Busy Or IE.readyState <> 4
            Application.Wait DateAdd("s", 1, Now)
        Loop

            Application.Wait DateAdd("s", 3, Now)
            Set IE = GetIE("https://gru.inpi.gov.br/pePI/jsp/patentes/PatenteSearchBasico.jsp")

            Application.Wait DateAdd("s", 3, Now)
            IE.document.getElementsByClassName("basic")(0).Value = "intRow" & intRow


        End With
    End If
    Next
End Sub


Function GetIE(sLocation As String) As Object

        Dim retVal As Object
        Dim my_url As String, my_title As String
        Set objShell = CreateObject("Shell.Application")
        IE_count = objShell.Windows.Count

        'loop through the window and find the tab


        For x = 0 To (IE_count - 1)
            On Error Resume Next
            'get the location and title
            my_url = objShell.Windows(x).document.Location
            my_title = objShell.Windows(x).document.Title

            'debug to check the value
            Debug.Print x
            Debug.Print my_url
            'find the special tab based on the title.
            If my_url Like sLocation Then
                Set retVal = objShell.Windows(x)
                'IE.Quit 'call the Quit method to close the tab.
                'Exit For   'exit the for loop
            Else
            End If
        Next

    Set GetIE = retVal

End Function

运行上述脚本后,它将打开三个选项卡并在登录输入文本中填充一个值。 请看下面的截图:

在此处输入图片说明

参考:

Web 自动化中使用的常用 VBA 方法和属性

暂无
暂无

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

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