Jump to content
nassausky

Closing all IE tabs except a selected one

Recommended Posts

nassausky

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

Share this post


Link to post
Share on other sites
Earthshine

I use loops to wait. That way it’s dynamic.  I detest and refused to use static waits Because they are wasteful in most cases. 

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
nassausky

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

Share this post


Link to post
Share on other sites
Danp2

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.

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

  • Similar Content

    • SkysLastChance
      By SkysLastChance
      I was wondering how I read data inside of a iframe. I would like to be able to click something inside a iframe. However, I can't even find the tag. 
      The id and name change all the time so I can't use those. 
      The code highlighted in blue is what I am trying to click. (second picture)
      Here is what I have tried.
      $target = "" $tags = $oIE.document.GetElementsByTagName("div") For $tag in $tags $class_value = $tag.GetAttribute("class") If string($class_value) = "s_92 altstyle s_93 s_94" Then $target = $tag ConsoleWrite("Tag Found " & $target.outerText&@CRLF) ExitLoop EndIf Next  

       
       

    • SkysLastChance
      By SkysLastChance
       
      WinActivate("MEDITECH - Internet Explorer") Sleep (500) $oIE = _IEAttach("MEDITECH") $oDiv1 = _IEGetObjById($oIE, "sysmenu-searchbarbutton") _IEAction($oDiv1, "click") I am just trying to click the little magnifying glass, next to the gear button with no luck. I was hoping someone might have an idea why this is not working?
       

    • SkysLastChance
      By SkysLastChance
      I am trying to type a name in patient summary search box. How should I go about doing this. I am clearly not doing it right. (Attached Picture of HTML code.)
      This is what I have tried. I assume this is not working because it is not a form? 
      WinActivate("MEDITECH - Internet Explorer")       Sleep (500)    $oIE = _IEAttach("MEDITECH")    $oForm = _IEFormGetObjByName($oIE, "systemMenu")    $oText = _IEFormElementGetObjByName($oForm, "sysmenu-searchbarinput") _IEFormElementSetValue($oText, "Hey! This works!")
    • Miliardsto
      By Miliardsto
      Hello I got problem with final program .exe closing on windows 7 ultimate 64bit. It starts , icon shows on toolbar and program exit.
      I have seen things like that before but it was caused by AV. There is no AV and UAC and firewall off.
      Maybe is there needed any microsoft library or etc or whatever?
       
    • aiter
      By aiter
      Just want to report a warning I get when using IE udf and running from F5 in scite

      -> IE.au3 T3.0-2 Warning from function internal function __IEIsObjType, Cannot register internal error handler, cannot trap COM errors (Use _IEErrorHandlerRegister() to register a user error handler)
      When I look at IE.au3 I see a comment that this function has been removed as there are no more fatal errors.
       
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.