Jump to content
Sign in to follow this  

IE Tabbed Browsing

Recommended Posts



I've been working on a script for work and have run into a brick wall trying to manage tabs in IE8. I've searched around and it seems there is no easy solution, or rather no solution that is within my current capabilities because DaleHomn referenced a lengthy workaround I couldn't quite grasp along with some very valuable information... so I created my own lengthy workaround ;)

For my specific requirement the script is going to initiate the instance of IE that I will manipulate, so the hwnd will be known. As long as the user doesn't change the order of the tabs manually after they are opened I can resolve the correct control key to use. If they decide they want to see google as the first tab even though they opened yahoo in the first tab I have no way of knowing based on the method below... does anyone have any ideas???

To test, just open a bunch of tabs, or even multiple IE windows with multiple tabs, and run the code then reorder your tabs manually and run again... note that the msgbox text refers to the original order, not what is currently displayed. Thanks in advance for any input.

#include <IE.au3>
Local $aIE[1][4], $oIE_Prev_hwnd, $oIE, $oIE_hwnd, $i_TabCTRLNum, $oIE_LocationName, $i_IENum
$aIE[0][0] = 0
Local $i_Tabs = 1
While 1
$oIE = _IEAttach("", "instance", $i_Tabs) ; attach to each TAB in each open instance of IE
If @error = $_IEStatus_NoMatch Then ExitLoop ; if there are no more tabs remaining to loop through
$oIE_hwnd = _IEPropertyGet($oIE,"hwnd") ; get hwnd of the window corresponding to this tab
If $oIE_hwnd <> $oIE_Prev_hwnd Then ; determine if the hwnd/window is different from the last tab
  $i_TabCTRLNum = 1 ; used to determine what control key to use in the control send function, if hwnd changed this is reset
  $i_IENum += 1
  $oIE_Prev_hwnd = $oIE_hwnd
$oIE_LocationName = _IEPropertyGet($oIE,"LocationName") ; this array should be dimmed to size according to your needs. for the purpose of this demo I'm using LocationName
ReDim $aIE[$i_Tabs + 1][4]
$aIE[$i_Tabs][0] = $oIE
$aIE[$i_Tabs][1] = $oIE_hwnd
$aIE[$i_Tabs][2] = $oIE_LocationName
$aIE[$i_Tabs][3] = '"^'&$i_TabCTRLNum&'"' ; for use in the controlsend function to select a specific tab in a specific instance of IE
$aIE[0][0] = $i_Tabs ; the number of open TABS
$aIE[0][1] = $i_IENum ; the number of instances of IE
$i_Tabs += 1 ; open tab counter
$i_TabCTRLNum += 1 ; tab control number counter
For $i = 1 To UBound ($aIE) - 1
WinActivate($aIE[$i][1]) ; activates a specific instance of IE
WinActivate($aIE[$i][1]) ; I added this because with 10 tabs mixed HTTP and HTTPS open across 5 instances of IE
;sometimes the window wouldn't take focus even though the winwaitactive released the script
ControlSend($aIE[$i][1],"","DirectUIHWND1",$aIE[$i][3]) ; selects a specific tab in a specific instance of IE
MsgBox(0,"","the "&$aIE[$i][2]&" window should now be in focus")
; **Please Note ** if the tabs are reordered the control keys correspond to the new order but the order of
;windows returned by _IEAttach is still the original order, the script is broken by manually reordering tabs

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this