Search code examples
internet-explorervbscriptqtphp-uft

How can I get the handle of the IE tab that has focus?


Does anyone know of a way to get the handle of the IE tab that has current focus using vbscript?

I'm trying to do some Automation using HP-UFT and need to get the handle of the tab that has focus so I can close it.

UFT treats each tab as it's own separate browser, so I'm able to get the collection of tabs but I have not been able to find a way to determine which one has the current focus.

The browser I am using is IE.


Solution

  • This answer will work only for IE.

    You do not need to get handle of particular tab. Just run a loop on number of opened tabs and check if it's visible or not. Delete if it's visible.

    Here is working sample code:

    iTab = Browser("CreationTime:=0").GetROProperty("number of tabs")
    For i = 0 To iTab - 1
        If Browser("CreationTime:=" & i).GetROProperty("visible") Then
            Print "Closing : " & Browser("CreationTime:=" & i).GetROProperty("name")
            Browser("CreationTime:=" & i).Close
            Exit For
        End If
    Next  
    

    This will close current focused tab.