Jump to content

Closing all IE tabs except a selected one


 Share

Recommended Posts

Hi all,

 

Anyone have any idea how to close all open tabs except a specific one I manually open.  Assuming I don't know what is open in all the tabs except just the one I want to keep open.

 

I didn't want to use sendkeys and I was trying to use the following code to list the title (or url) of the 3 open tabs and  after I got that part working I would just close the other 2. This sample only displays the title of the first open tab

#include <IE.au3>

Const $ie_new_in_tab = 0x0800
$oIE = _IECreate("https://www.autoitscript.com")
__IENavigate($oIE, "https://www.autoitscript.com/forum/", 1, $ie_new_in_tab) ;(obj,url,wait,param)
__IENavigate($oIE, "https://www.google.com/", 1, $ie_new_in_tab) ;(obj,url,wait,param)


Local $aIE[1]
$aIE[0] = 0

Local $i = 1, $oIE
While 1
    $oIE = _IEAttach("", "instance", $i)
    If @error = $_IEStatus_NoMatch Then ExitLoop
    ConsoleWrite(_IEPropertyGet($oIE, "title") & @CRLF)
    ReDim $aIE[$i + 1]
    $aIE[$i] = $oIE ;each item holds object
    $aIE[0] = $i ;first item holds count
    $i += 1
WEnd

MsgBox($MB_SYSTEMMODAL, "Browsers Found", "Number of browser instances in the array: " & $aIE[0])

; This doesn't return the list of tabs in the console just the first tab

 

Thanks for any and all help

Link to comment
Share on other sites

Thanks Danp2 & Earthshine. According to the docs, _IECreate waits for the page to load by default. I tried waiting using both methods in the past but not cleanly so I figured I would try it again like you both suggested.

First using a large sleep of 5 seconds after all the navigations took place and the first page loaded up alone and the program sat in a long sleep cycle. I would of force killing the autoit script after about 40 seconds.  

Second I remarked the sleep command then added _IELoadWait after each navigation but _IELoadWait loads the tabs immediately and still shows only 1 result . Here is what I tried the 2nd time.

#include <IE.au3>

Const $ie_new_in_tab = 0x0800
$oIE = _IECreate("https://www.autoitscript.com")
_IELoadWait($oIE)
__IENavigate($oIE, "https://www.autoitscript.com/forum/", 1, $ie_new_in_tab) ;(obj,url,wait,param)
_IELoadWait($oIE)
__IENavigate($oIE, "https://www.google.com/", 1, $ie_new_in_tab) ;(obj,url,wait,param)
_IELoadWait($oIE)
;Sleep(5000)


Local $aIE[1]
$aIE[0] = 0
Local $i = 1, $oIE
While 1
    $oIE = _IEAttach("", "instance", $i)
    If @error = $_IEStatus_NoMatch Then ExitLoop
    ConsoleWrite(_IEPropertyGet($oIE, "title") & @CRLF)
    ReDim $aIE[$i + 1]
    $aIE[$i] = $oIE ;each item holds object
    $aIE[0] = $i ;first item holds count
    $i += 1
WEnd
MsgBox($MB_SYSTEMMODAL, "Browsers Found", "Number of browser instances in the array: " & $aIE[0])
; This doesn't return the list of tabs in the console just the first tab

 

Not giving up I know both of you seem to have tried this before so I did it again with just the 5 second sleep after the Create & Navigation commands but this time it worked like a charm.  Odd. I tried it a few more times and now it's working as so

#include <IE.au3>

Const $ie_new_in_tab = 0x0800
$oIE = _IECreate("https://www.autoitscript.com")
;_IELoadWait($oIE)
__IENavigate($oIE, "https://www.autoitscript.com/forum/", 1, $ie_new_in_tab) ;(obj,url,wait,param)
;_IELoadWait($oIE)
__IENavigate($oIE, "https://www.google.com/", 1, $ie_new_in_tab) ;(obj,url,wait,param)
;_IELoadWait($oIE)
Sleep(3000)


Local $aIE[1]
$aIE[0] = 0
Local $i = 1, $oIE
While 1
    $oIE = _IEAttach("", "instance", $i)
    If @error = $_IEStatus_NoMatch Then ExitLoop
    ConsoleWrite(_IEPropertyGet($oIE, "title") & @CRLF)
    ReDim $aIE[$i + 1]
    $aIE[$i] = $oIE ;each item holds object
    $aIE[0] = $i ;first item holds count
    $i += 1
WEnd
;MsgBox(0, "Browsers Found", "Number of browser instances in the array: " & $aIE[0])
; This doesn't return the list of tabs in the console just the first tab

Great now my question to everyone is how can I get _IELoadWait to work correctly as method 1 above doesn't but instead skips to the loop after it and doesn't recognize all the open tabs

OK working on it a bit, I wanted to figure it out on my own.. (You guys are great though), I went through the IE.AU3 file and it wasn't documented really but I got an idea that the 2nd parameter was maybe if I wanted to pause the loading and the 3rd parameter was a timeout value which might wait longer than the default. 

#include <IE.au3>

Const $ie_new_in_tab = 0x0800
$oIE = _IECreate("https://www.autoitscript.com")
;_IELoadWait($oIE,0,5000)
__IENavigate($oIE, "https://www.autoitscript.com/forum/", 1, $ie_new_in_tab) ;(obj,url,wait,param)
_IELoadWait($oIE,0,5000)
__IENavigate($oIE, "https://www.google.com/", 1, $ie_new_in_tab) ;(obj,url,wait,param)
_IELoadWait($oIE,0,5000)



Local $aIE[1]
$aIE[0] = 0
Local $i = 1, $oIE
While 1
    $oIE = _IEAttach("", "instance", $i)
    If @error = $_IEStatus_NoMatch Then ExitLoop
    ConsoleWrite(_IEPropertyGet($oIE, "title") & @CRLF)
    ReDim $aIE[$i + 1]
    $aIE[$i] = $oIE ;each item holds object
    $aIE[0] = $i ;first item holds count
    $i += 1
WEnd
;MsgBox(0, "Browsers Found", "Number of browser instances in the array: " & $aIE[0])
; This doesn't return the list of tabs in the console just the first tab

 

Hmm  it's still not consistent so for now until I get a more definitive answer why it isn't waiting correctly I added a little more delay to the _IELoadWait as so

#include <IE.au3>

Const $ie_new_in_tab = 0x0800
$oIE = _IECreate("https://www.autoitscript.com")
_IELoadWait($oIE,250,3000)
__IENavigate($oIE, "https://www.autoitscript.com/forum/", 1, $ie_new_in_tab) ;(obj,url,wait,param)
_IELoadWait($oIE,250,3000)
__IENavigate($oIE, "https://www.google.com/", 1, $ie_new_in_tab) ;(obj,url,wait,param)
_IELoadWait($oIE,250,3000)



Local $aIE[1]
$aIE[0] = 0
Local $i = 1, $oIE
While 1
    $oIE = _IEAttach("", "instance", $i)
    If @error = $_IEStatus_NoMatch Then ExitLoop
    ConsoleWrite(_IEPropertyGet($oIE, "title") & @CRLF)
    ReDim $aIE[$i + 1]
    $aIE[$i] = $oIE ;each item holds object
    $aIE[0] = $i ;first item holds count
    $i += 1
WEnd
MsgBox(0, "Browsers Found", "Number of browser instances in the array: " & $aIE[0])

 

Edited by nassausky
I had to take back my solution since it wasn't consistent
Link to comment
Share on other sites

I believe the issue you are running into is that $oIE always points to the first tab and not at any of the subsequent tabs. So each call to _IELoadWait is checking the first tab, instead of the most recent one. You will have to get an object reference to the desired tab before you can check it's status with _IELoadWait.

Link to comment
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
 Share

×
×
  • Create New...