【问题标题】:VBA Automation isn't working on new IE tabVBA 自动化不适用于新的 IE 选项卡
【发布时间】:2020-03-26 17:30:46
【问题描述】:

这是我的第一个问题,我是 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

【问题讨论】:

  • 您能否详细说明“不起作用”的含义。应该发生什么以及上面的代码中发生了什么?你有错误吗?
  • 当然,我在假装浏览 trought 选项卡并从 excel 文件中填写输入内容。我设法通过新窗口做到这一点,但不是通过同一个选项卡。吕志的回答,效果很好。

标签: excel vba internet-explorer


【解决方案1】:

根据您的代码和网站,您似乎要打开多个选项卡(具有相同的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

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

参考:

Common VBA Methods & Properties used in web automation

【讨论】:

  • .Visible = True 已设置,因此脚本后面不需要。我还将使用 IIF 来测试 intRow 的值以确定是否在新选项卡中打开,从而取消整个 else 部分。
  • 非常感谢,代码可以满足我的要求。巴西专利局网站访问出现异常。当我第一次直接通过链接访问时:gru.inpi.gov.br/pePI/jsp/patentes/PatenteSearchBasico.jsp 我被重定向到这个登录页面,但是当我通过另一个页面inpi.gov.br 访问相同的链接时,点击右上角的“Faça uma busca” ,然后我转到我想要的页面。随后的访问,即使通过直接链接,也会导致我想要的页面而不是这个登录页面。再次感谢您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多