模拟点击后如何选择IE标签页

时间:2019-07-01 11:43:27

标签: vba windows internet-explorer

我正在制作一个简单的脚本,该脚本可以打开一个网站,找到一个按钮,单击它(打开一个新标签),然后在新标签上查找数据。我被困在转储点:如何访问新标签上的数据? 非常感谢!

Sub testWebOpenTabAndFocus()    
    Dim i As Long
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object

    ' open new IE window
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True

    ' Send the form data To URL As POST binary request
    IE.navigate "somesite.com"

    '..... some code to search about a button

    objElement.Click    ' click button to load a new tab

    ' Wait while IE re-loading...
    While IE.Busy
            DoEvents
    Wend

    ' finding the new tab with Windows(x)

    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_title = objShell.Windows(x).Document.Title
        If my_title Like "*Analysis Application*" Then 
            Set IE = objShell.Windows(x) 'this is my new tab
        End If
    Next

    'Some code to be runned on the new tab
    'but its not working because it still has the focus on previous tab

    'Unload IE
    Set IE = Nothing
    Set objElement = Nothing
    Set objCollection = Nothing
End Sub

1 个答案:

答案 0 :(得分:2)

尝试在上面的代码中替换下面的代码部分,可能有助于解决该问题。

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
         If x.Name = "Internet Explorer" And TypeName(x.document) = "HTMLDocument" Then
        my_title = objShell.Windows(x).document.Title
        Debug.Print (my_title)
        If my_title Like "*Analysis Application*" Then
            Set IE = objShell.Windows(x) 'this is my new tab
        End If
         End If
    Next

如果问题仍然存在,而不是尝试提供详细信息,例如您在哪一行上遇到错误或停留在错误消息中,则可能会让我们对问题有更多了解。