Jump to content
Sign in to follow this  
DaleHohm

Attaching to an existing IE instance - one method

Recommended Posts

You cannot use ObjGet on InternetExplorer because its instances are not placed into the ROT (Running Object Table). Here is one method of attaching to a desired, existing instance of IE by examining the URL it is displaying.

I do hope there are better ways to do this, but this works.

It is important to know that you can get to the InternetExplorer object both with an $ObjIE = ObjCreate("InternetExplorer.Application") which creates a new instance, or through the Shell.Application object to Shell.Application.Windows.Item as done in the code below.

If you set the $myURL variable to the exact URL being displayed in an existing IE window, you will end up with $ObjIE pointing to that instance. You can then use the properties, methods and events of the InternetExplorer object to manipulate it (see MSDN InternetExplorer Object Reference for more information.

Dim $ObjShell = ObjCreate("Shell.Application")
Dim $ObjShellWindows = $ObjShell.Windows(); collection of all ShellWindows (IE and File Explorer)
Dim $ObjIE

; URL of IE instance we want to find
Dim $myURL = "http://www.autoitscript.com/"

For $item = 0 to ($ObjShellWindows.count - 1)
    $ObjIE = $ObjShellWindows.item($item); this is an InternetExplorer object
    ConsoleWrite($item & "> " & $ObjIE.path & @CR)
; The $ObjIE.path appears to be "C:\Program Files\Internet Explorer\" for IE and
; "C:\WINDOWS\" for file explorer on my machine (ShellWindows can be either).  
; File explorer does not have the same properties as IE, so it is important to know.
; There is certainly a better way to figure this out, but I haven't found it yet.
    If ($ObjIE.path = @ProgramFilesDir & "\Internet Explorer\") Then
        ConsoleWrite($item & "> " & $ObjIE.document.URL & @CR)
        If ($ObjIE.document.URL = $myURL) Then ExitLoop; We found it...
        $ObjIE = ""
    Else
        $ObjIE = ""
    EndIf
Next

If isObj($ObjIE) Then
    ConsoleWrite("--> $ObjIE now points to an IE Instance connected to " & $myURL & @CR)
Else
    ConsoleWrite("--> No IE instance was found connected to " & $myURL & @CR)
EndIf
Edited by DaleHohm

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

Dale,

Thanks for this useful tip.

It was already part of the beta versions. See in the beta.ZIP file in tests\com\Instance-of-IE-test.au3

See also: http://www.autoitscript.com/forum/index.php?showtopic=10444

Regards,

-Sven

<{POST_SNAPBACK}>

Cool. And the use of LocationURL negates the need to look at .path to differentiate between IE and File Explorer... also no need for looking at the .item

New code can be simplified to:

Dim $ObjShell = ObjCreate("Shell.Application")
Dim $ObjShellWindows = $ObjShell.Windows(); collection of all ShellWindows (IE and File Explorer)
Dim $ObjIE

; URL of IE instance we want to find
Dim $myURL = "http://www.autoitscript.com"

For $Window in $ObjShellWindows
; ConsoleWrite($window.LocationURL & @CR)
    If $window.LocationURL = $myURL Then
        $ObjIE = $Window
        ExitLoop; We found it...
    Else
        $ObjIE = 0
    EndIf
Next

If isObj($ObjIE) Then
    ConsoleWrite("--> $ObjIE now points to an IE Instance connected to " & $myURL & @CR)
Else
    ConsoleWrite("--> No IE instance was found connected to " & $myURL & @CR)
EndIf

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

Sweet guys, no need for pixelsearch any more :(


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

Nice one. I had a few systems that had a bit of a problem, so I shortened it a bit and put in in a function.

func IE_connect()
; returns the internet explorer object if sucessful or 0 if it fails.
; mainly from DaleHohm's code

Dim $ObjShell = ObjCreate("Shell.Application")
Dim $ObjShellWindows = $ObjShell.Windows(); collection of all ShellWindows (IE and File Explorer)
Dim $ObjIE

For $Window in $ObjShellWindows
    If StringInStr($window.LocationURL,"https:")>0 or StringInStr($window.LocationURL,"http:")>0 or StringInStr($window.LocationURL,"ftp:")>0 Then
        $ObjIE = $Window
        ExitLoop; We found it...
    Else
        $ObjIE = 0
    EndIf
Next

If isObj($ObjIE) Then
    ConsoleWrite("--> $ObjIE now points to an IE Instance connected" & @CR)
Else
    ConsoleWrite("--> No IE instance was found connected" & @CR)
EndIf
Return $ObjIE
EndFunc

Now I just need to find/make one for firefox like this.


AutoIt3, the MACGYVER Pocket Knife for computers.

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...