VBA Automation无法在新的IE标签上使用

时间:2020-03-26 03:17:53

标签: excel vba internet-explorer

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

1 个答案:

答案 0 :(得分:0)

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

  1. 如何访问网页元素。
  2. 如何切换标签页并将焦点集中到新标签页上。

要访问网页元素,请从您的网页资源中(使用F12开发人员工具进行检查),我注意到该表包含6行,但最后一行不包含input元素。因此,使用您的代码,我找不到输入元素。我发现输入元素包含class属性。因此,我建议您可以使用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

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

enter image description here

参考:

Common VBA Methods & Properties used in web automation

相关问题