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

    • XaelloNegative
      By XaelloNegative
      Hi,
      So, I am trying to automate and simplify the retrieval of data from our company website (made by an outside company). I've had experience with IE manipulation via autoIt however, this one is a bit tricky for me.
      I have a table that has 81 cells which are buttons. What im trying to get is the information in "data-content" tag. 
      <a id="bookedVehicleLinkButton" class="btn btn-sm btn-block btn-xsm btn-success" data-toggle="popover" data-placement="bottom" data-original-title="Trip Details" data-container="body" data-content="Plate No: UGQ-857<br />Model: TOYOTA GRANDIA GL" href="javascript:__doPostBack('ctl00$ctl00$masterContentPlaceholder$reservationContentPlaceholder$bookingDataList$ctl00$vehicleDataList$ctl28$bookedVehicleLinkButton','')">IXARA (LITO SULIT)</a> Tried using the following:
      $oTagsCell = _IETagNameGetCollection($oIE, "td") For $item in $oTagsCell $test = _IEPropertyGet($item, "innertext") ClipPut($test) Next $oTagsTable = _IETagNameGetCollection($oIE, "table") For $item in $oTagsTable $test = _IEPropertyGet($item, "innertext") ClipPut($test) Next $oTagsLink = _IETagNameGetCollection($oIE, "a") For $item in $oTagsLink $test = _IEPropertyGet($item, "innertext") ClipPut($test) Next I don't receive any errors from my aforementioned attempts however no luck in getting those "data-contents". Attached is a screenshot, I do the looking of elements in chrome but the codes are for IE. And if you might ask, the cells (or buttons) have the same ID.
       
      Thank you guys for any inputs.
       
    • XinYoung
      By XinYoung
      Quick question: How can I click this "Select All" button in IE? There's no ID, and the href is "#"  
      Am I looking at the wrong html code?
      Am I looking
      Thank you all in advance!
    • Jemboy
      By Jemboy
      Hi,
      At work we have some proprietary website, users have to login to.
      I have "made" an autoit executable to start IE, go the website, login, so the user do not have input their credentials every time.
      By NDA I am not allowed disclosed the URL of the website nor the login credentials
      So I made a fake website and an autoitscript to illustrate my question.
      #include <ie.au3> $oIE = _IECreate ("about:blank", 0, 1, 1, 1) $HWND = _IEPropertyGet($oIE, "hwnd") WinActivate ($HWND,"") WinSetState($HWND, "", @SW_MAXIMIZE) _IENavigate ($oIE, "http://demo.rkilinc.nl",1) The above start my demo website. The actual website has some links in the footer that I do not want most people click on.
      I contacted the developers of the website and they are thinking of making an option to configure what links to show in the footer, but they said it's not a high priority for them.
       
      I discovered, that by click F12 and deleting the <footer> element the footer is delete from the live page view (until the page is reloaded off course)
      I want to automate the removal of the footer, without using things like send().
      I tried getting the footer with _IEGetObjById and deleting it with _IEAction, but that didn't work.
      Does any one has an idea how I could delete the footer directly from view with an autoit script?
      TIA, Jem.
       
       

    • XinYoung
      By XinYoung
      HI! ... this is a big one (at least for me) 
      You guys previously helped me copy the used range in column A and paste them into a Website one at a time in a loop. Cool! Now, for another function, I have 2 columns, A and B, and two input boxes in the Website. I'm having a hard time replicating the loop for the 2 columns. 
      This is how I'm opening the Excel workbook (copied from the previous function that only had 1 column). I need to also get the used range in column B.
      Func OpenExcelForCopy() Global $aBBTableData Global $oExcel = _Excel_Open() Global $oWorkbook = _Excel_BookOpen($oExcel, $ChosenFileName, Default, True, True) $oExcel.Sheets("CopyCourses").Activate ;~ Get all used cells in column A:A Global $aSearchItems = _Excel_RangeRead($oWorkbook, 1, $oWorkbook.Sheets("CopyCourses").Usedrange.Columns("A:A")) ;~ Duplicate the $aSearchItems Array Global $aSearchResult = $aSearchItems ;~ Loop through the array starting at 0 until the end of the array which is (Ubound($aSearchItems) - 1) For $i = 0 To UBound($aSearchItems) - 1 $aSearchResult[$i] = SearchCourseForCopy($aSearchItems[$i]) Next _Excel_RangeWrite($oWorkbook, Default, $aSearchResult, "C1") Finished() EndFunc ;==>OpenExcelForCopy Then we eventually get here. I don't think anything needs to change here but I'm not sure. This is where I paste the data from Column A into an input field (which is a search tool in a website). If the search is good, then we get to the tricky part...
      ;~ OK, we logged in and we searched for a course. Lets COPY it! Func CopyCourseBegin() Local $sResult $iSearchIndex = _ArraySearch($aBBTableData, "Course ID", 0, 0, 0, 1, 1, 0) ;~ If the course was not found, do this. If $iSearchIndex = -1 Then ;~ MsgBox(4096, "Search Error", "Item not found") $sResult = "Source Not Found" _Excel_RangeWrite($oWorkbook, Default, $aSearchResult, "C1") ;~ Now go back to the Excel sheet and search for the next one. ;~ If the course was found, begin the COPY process. Else For $i = 0 To UBound($aSearchItems) - 1 $aSearchResult[$i] = CopyCourseNow($aSearchItems[$i]) Next $sResult = "Copied" _Excel_RangeWrite($oWorkbook, Default, $aSearchResult, "C1") EndIf Return $sResult EndFunc ;==>CopyCourseBegin This is the "tricky part" where I'm confused. I can copy and paste what's in column A just fine, but I can't manage to replicate it for column B. I need to paste whats in Column B into "destinationCourseId"
      ;~ The course search was successful. COPY the course now. Func CopyCourseNow($_sSearchResult) ;~ Navigate to the course copy page. _IENavigate($oIE, $urlBBCourseCopy) ;~ Copy the SOURCE course ID from the Excel sheet ;~ Paste whats copied from column A into the Source Course ID text box Local $oForm = _IEGetObjByName($oIE, "selectCourse") Local $oSearchString = _IEFormElementGetObjByName($oForm, "sourceCourseId") _IEFormElementSetValue($oSearchString, $_sSearchResult) ;~ Paste whats copied from column B into the Destination Course ID text box ?!?!?!?! Local $oForm = _IEGetObjByName($oIE, "selectCourse") Local $oSearchString = _IEFormElementGetObjByName($oForm, "destinationCourseId") _IEFormElementSetValue($oSearchString, $_sSearchResult) ;~ Just exit cause im stuck :( _Exit() EndFunc ;==>CopyCourseNow After I paste the data from column A into "sourceCourseId" and column B into "destinationCourseId", I'll make it do some stuff. Then I need it to loop around until the used ranges in column A & B is finished.
      Does the entire code need to change now that there's two columns?
       
       
    • Dequality
      By Dequality
      I don't really have any code cuz whatever i try it doesnt work -.- 
      Can anyone please make me a sample i can study? <,< i need to make a simple script to open explorer click some objects on the site , rinse repeat.. a example with autoit' website is just fine i just need to study it.. the i can't seem to find any yt video on this.. '-_-
      And yes i tried looking at the manual .. but i dont get anything from it <.<'
       
      Any help highly appreciatet <3 
×