separate IE instances?

C

Chris Curvey

I need to create a set of IE instances that have different sets of
session cookies. I thought that using the win32com.DispatchEx function
would do this, but it doesn't seem to. In other words

ie1 = win32com.DispatchEx("InternetExplorer.Application")
ie2 = win32com.DispatchEx("InternetExplorer.Application")

gives me two IE windows, but only one IEXPLORE process in Task Manager.
And if I login to a site that uses session cookies to track sessions
using ie1, when I move ie2 to the same site, ie2 is already logged in.

Any help appreciated.

-Chris
 
J

J Correia

Chris Curvey said:
I need to create a set of IE instances that have different sets of
session cookies. I thought that using the win32com.DispatchEx function
would do this, but it doesn't seem to. In other words

ie1 = win32com.DispatchEx("InternetExplorer.Application")
ie2 = win32com.DispatchEx("InternetExplorer.Application")

gives me two IE windows, but only one IEXPLORE process in Task Manager.
And if I login to a site that uses session cookies to track sessions
using ie1, when I move ie2 to the same site, ie2 is already logged in.

Any help appreciated.

-Chris

from win32com.client import Dispatch

ie1 = pythoncom.CoCreateInstance("InternetExplorer.Application", None,\
pythoncom.CLSCTX_SERVER,
pythoncom.IID_IDispatch)
ie1 = Dispatch(ie1)
ie2 = pythoncom.CoCreateInstance("InternetExplorer.Application", None,\
pythoncom.CLSCTX_SERVER,
pythoncom.IID_IDispatch)
ie2 = Dispatch(ie2)
..
..
..
 
C

Chris Curvey

Bummer. No change at all. (In fact, I can't even call Navigate()
without throwing an error). I'm on win2k, if that makes any difference.
 
M

Martin Franklin

Chris said:
Bummer. No change at all. (In fact, I can't even call Navigate()
without throwing an error). I'm on win2k, if that makes any difference.


I could be way off, but isn't windows one of those OS's that doesn't
allow you to have two instances of IEXPORE.EXE running IOW the OS is
preventing you from running two instances of this executable. Someone
with a lot more knowledge of windows internals will I'm sure come along
and correct me ;-)

Mart
 
C

Chris Curvey

I would have given up on this a long time ago, but I can create two
IEXPLORE processes simultaneously (and get the behavior I want) by just
manually launching them from the Start menu. (Of course, that doesn't
mean that I can launch them programmatically, but I'm hoping that
someone can give me a definitive answer.)
 
J

J Correia

Chris Curvey said:
I would have given up on this a long time ago, but I can create two
IEXPLORE processes simultaneously (and get the behavior I want) by just
manually launching them from the Start menu. (Of course, that doesn't
mean that I can launch them programmatically, but I'm hoping that
someone can give me a definitive answer.)

Right, I hadn't quite understood your problem when I posted my reply. The code
posted does work and allow navigation, etc. but you do have the problem with it
sharing the same session cookies (I'm also on Win2k). And to answer Martin, you
can definitely create as many iexplore.exe instances as you like in Windows.

How to get Python to launch several instances with COM... not sure, although I'm
99% certain it is doable. I'll hunt around and see if I can find a solution
which I'll post back.

J
 
J

J Correia

J Correia said:
Right, I hadn't quite understood your problem when I posted my reply. The code
posted does work and allow navigation, etc. but you do have the problem with it
sharing the same session cookies (I'm also on Win2k). And to answer Martin, you
can definitely create as many iexplore.exe instances as you like in Windows.

How to get Python to launch several instances with COM... not sure, although I'm
99% certain it is doable. I'll hunt around and see if I can find a solution
which I'll post back.

A very quick and very, very dirty method which might work is to start up
the instances as follows:
import win32api
a = win32api.ShellExecute(0,None,"iexplore.exe",None,None,1)
b = win32api.ShellExecute(0,None,"iexplore.exe",None,None,1)
This creates the 2 instances of iexplore.exe in Windows you're looking for.

Then use code like this to attach to the already running instances:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/269345

Haven't tried it because I'm certain there's a much more elegant solution, but
depending on how quickly you need to get going it might be a possible short term
work around until someone posts the better way. Perhaps you can also post some
more info on what you're actually trying to achieve... make it easier for
someone to
help or even suggest alternatives.

JC
 
C

Chris Curvey

thanks for all the help. I'll give the ShellExecute() approach a try
in the morning.

The short version of what I'm trying to do is....

Have my website login to a 3rd party website on behalf of my customer,
fill out a form, and submit it. I'm just using CGI to keep things
simple, but overlapping requests from different customers are the
problem. The 3rd party site uses a lot of javascript, so mechanize
isn't going to work. I could use some kind of locking mechanism and
"single-thread" access to IE, but that won't scale. I guess the next
approach would be to queue the requests and have a pool of worker
processes (running as different users) process the requests and report
back.
 
J

J Correia

Chris Curvey said:
thanks for all the help. I'll give the ShellExecute() approach a try
in the morning.

The short version of what I'm trying to do is....

Have my website login to a 3rd party website on behalf of my customer,
fill out a form, and submit it. I'm just using CGI to keep things
simple, but overlapping requests from different customers are the
problem. The 3rd party site uses a lot of javascript, so mechanize
isn't going to work. I could use some kind of locking mechanism and
"single-thread" access to IE, but that won't scale. I guess the next
approach would be to queue the requests and have a pool of worker
processes (running as different users) process the requests and report
back.

You might have a specific reason for using COM, but if not, have you
considered using the python urllib or urllib2 modules instead?
It should overcome the session cookie / overlapping request issues i think.
 
E

erinhouston

Here is quick and dirty example of what jc talked about.

import win32api
from win32com.client import Dispatch

a = win32api.ShellExecute(0,None,"iexplore.exe",None,None,1)
internetExplorerClassIdentity='{9BA05972-F6A8-11CF-A442-00A0C90A8F39}'
hwnds = Dispatch(internetExplorerClassIdentity)
ieObj = hwnds[1]
ieObj.Navigate("http://www.google.com/search?hl=en&lr=&q=python")
 
C

Chris Curvey

the good news is that if run the ShellExecute bit twice, I get two
instances of explorer in Task Manager

The bad news is that the line "ioObj = hwnds[1]" tells me either "This
object does not support enumeration" (if I have not used makepy to
generate the classes) or "object has no attribute __getitem__" (if I
have used makepy)

looking at the hwnds itself, it appears to be a

"win32com.gen_py.Microsoft Internet Controls.IWebBrowser2 instance"

If I call hwnds.Navigate("http://www.google.com"), I don't get an
error, but I also don't see anything change in IE.
 
E

erinhouston

Sorry about that I had an instance of ie running in the background that
was version 0 didn't see the problem tell this morining when my
computer started to not work. it should be hwnds[1] should be
hwnds[0]. I woud enumerate them like in the code below.

If you have ie as your default browser you can use a
shellExecute(0,None,"some url here", None, None, 1)
To open directley to that page.

Here is a more complete script:
import win32api, time
from win32com.client import Dispatch
a = win32api.ShellExecute(0,None,"iexplore.exe",
"www.ishpeck.net",None,1)
b = win32api.ShellExecute(0,None,"iexplore.exe",
"www.google.com",None,1)
internetExplorerClassIdentity='{9BA05972-F6A8-11CF-A442-00A0C90A8F39}'
hwnds = Dispatch(internetExplorerClassIdentity)
# way I showed you before dosn't work verry well if more then one ie is
open
#ieObj = hwnds[0]
#ieObj.Navigate("http://www.google.com/search?hl=en&lr=&q=python")
time.sleep(30)
for i in range(hwnds.Count):
.....if hwnds.LocationURL.lower().find("ishpeck") > -1:
.........ieObj1 = hwnds
.....if hwnds.LocationURL.lower().find("google") > -1:
.........ieObj2 = hwnds

ieObj1.Navigate("http://www.google.com/search?hl=en&lr=&q=python")
ieObj2.Navigate("http://groups-beta.google.com/group...2dba6/343672a01d5b2cdc?hl=en#343672a01d5b2cdc")
 
J

J Correia

Sorry about that I had an instance of ie running in the background that
was version 0 didn't see the problem tell this morining when my
computer started to not work. it should be hwnds[1] should be
hwnds[0]. I woud enumerate them like in the code below.

If you have ie as your default browser you can use a
shellExecute(0,None,"some url here", None, None, 1)
To open directley to that page.

Here is a more complete script:
import win32api, time
from win32com.client import Dispatch
a = win32api.ShellExecute(0,None,"iexplore.exe",
"www.ishpeck.net",None,1)
b = win32api.ShellExecute(0,None,"iexplore.exe",
"www.google.com",None,1)
internetExplorerClassIdentity='{9BA05972-F6A8-11CF-A442-00A0C90A8F39}'
hwnds = Dispatch(internetExplorerClassIdentity)
# way I showed you before dosn't work verry well if more then one ie is
open
#ieObj = hwnds[0]
#ieObj.Navigate("http://www.google.com/search?hl=en&lr=&q=python")
time.sleep(30)
for i in range(hwnds.Count):
....if hwnds.LocationURL.lower().find("ishpeck") > -1:
........ieObj1 = hwnds
....if hwnds.LocationURL.lower().find("google") > -1:
........ieObj2 = hwnds

ieObj1.Navigate("http://www.google.com/search?hl=en&lr=&q=python")

ieObj2.Navigate("http://groups-beta.google.com/group/comp.lang.python/browse_thr
ead/thread/ba1395566452dba6/343672a01d5b2cdc?hl=en#343672a01d5b2cdc")

The thing to note is that hwnds brings back a list of all Internet Explorer
objects running on your machine. This *includes* things that might
not be obvious at first, like a simple Windows Explorer window
(or windows) you have running (since it uses IE in the background).
Printing hwnds.LocationName and hwnds.LocationURL in a loop
will show you exactly what processes are using IE at the time you run it.
Given that the returned list will vary each time you run your program
you'll definitely have to iterate through each hwnds item and check
if it is the browser session you want as shown in the above code.

JC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,241
Messages
2,571,223
Members
47,856
Latest member
mmorais

Latest Threads

Top