Welcome, guest | Sign In | My Account | Store | Cart

Various sources (but principally Hammond and Robinson) show how to instantiate Internet Explorer in order to have access to its interfaces. However, few places show how to connect to an instance that is already running, which is so easy in VB.

Thanks are due to someone called "gcash" for this idea. See http://dbforums.com/t867088.html.

It just seemed to me that this is a good recipe to have in this collection as a reference to a certain approach, rather than as a full solution.

Python, 12 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from win32com . client import Dispatch 
from win32gui import GetClassName

ShellWindowsCLSID = '{9BA05972-F6A8-11CF-A442-00A0C90A8F39}'
ShellWindows = Dispatch ( ShellWindowsCLSID )

for shellwindow in ShellWindows :
    if GetClassName ( shellwindow . HWND ) == 'IEFrame' :
        print shellwindow 
        print shellwindow . LocationName
        print shellwindow . LocationURL
        print 50 * '-'

The principal inadequacy of this recipe is its apparent failure to make it possible to sink events from IE. At the very least, one would need to be able to wait on 'DocumentComplete'.

As usual, no distinction is made between instances of Windows Explorer and Internet Explorer in the string that is printed for shellwindow, and both are identified as "Microsoft Internet Explorer" in the output from the first print statement in the script above. However, shell window's class can be used to select only Internet Explorer windows for display, as shown in the if statement.

5 comments

Graham Fawcett 17 years, 11 months ago  # | flag

waiting for DocumentComplete (sort of). One way to achieve something like a DocumentComplete event is to check the Busy attribute:

sw = ShellWindows[0]  # let's grab the first window
sw.Document.Location = 'http://www.python.org/'

while sw.Busy:        # loop until the page is loaded
    time.sleep(.1)
print sw.LocationName
Don Pelletier 17 years, 11 months ago  # | flag

You would think there would be an active attribute. You would think there would be an 'active' attribute that exists in Shell.Windows. So if you wrote a toolbar item it would be easy to find out from what instance of Internet Explorer were you started from. Or being a child component (a button on the ie browser bar) of an ie instance, you should be able to instantly tell who your parent browser instance is.

Don Pelletier 17 years, 11 months ago  # | flag

You would think there would be an active attribute. You would think there would be an 'active' attribute that exists in Shell.Windows. So if you wrote a toolbar item it would be easy to find out from what instance of Internet Explorer were you started from. Or being a child component (a button on the ie browser bar) of an ie instance, you should be able to instantly tell who your parent browser instance is.

Don Pelletier 17 years, 11 months ago  # | flag

You would think there would be an active attribute. You would think there would be an 'active' attribute that exists in Shell.Windows. So if you wrote a toolbar item it would be easy to find out from what instance of Internet Explorer were you started from. Or being a child component (a button on the ie browser bar) of an ie instance, you should be able to instantly tell who your parent browser instance is.

Eric Koome 17 years, 4 months ago  # | flag

Connecting to running instances of IE on your computer. You can see a similar recipe done using ctypes. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/305273