March 30th, 2004, 05:29 PM
Can't get to Internet Explorer COM object
I've been working on a python script that for testing web applications. It:
1) Opens up Internet Explorer
2) Loads a URL
3) Takes a screenshot
4) Compares the screenshot with a previous one
5) Alerts people by email if there is a discrepancy
6) Quits Internet Explorer
It repeats the above process for a number of URLs.
On my development box, it worked fine. When I moved it to a much slower box, with a fresh install of Windows 2k and Python, it started breaking at random:
It's breaking before it even gets to do a comparison of screenshots; at the point where it's taking the "good" screenshot.
Note that this doesn't necessarily happen at the same point in the loop. I started adding sleep() calls to give internet explorer time to fully start up, but I'm not sure if that's helping.
I suspect that this is because the new box is so much slower, but I'm not sure.
Here's the bit of code that opens the browser and saves the image:
I've highlighted the line Python is complaining about. I've tried increasing the sleep call to 5, but that didn't help (although it did repeat the cycle several more times before breaking).
def makeGoldenFile( self, app ):
ie = win32com.client.Dispatch( "InternetExplorer.Application" )
time.sleep( 2 )
ie.Visible = 1
ie.FullScreen = 1
ie.Navigate( app.url )
time.sleep( g_goldenFilesTimeout )
fn = app.getGoldenFilename()
cmd = "c:\\PROGRA~1\\CAP\\cap.exe /full /file=" + fn
os.system( cmd )
self.addIgnoreAreas( fn, app )
return self.compareScreenshots( fn, app.getGoldenFilename() )
March 31st, 2004, 02:49 AM
I have had the same problem scripting Excel from Python, and I agree with your diagnosis that you are trying to use the COM object before IE has fully initialised.
You could try looping until it works:
Of course if you accidentally tried this on an attribute that does not exist then it would loop forever, so either test it carefully or replace the 'while 1' with a 'for x in xrange(60)' to give a large timeout.
ie.Visible = 1
Incidentally, does it make a difference if there is already an instance of IE running? If it has to be loaded from scratch then the startup is going to take longer. The startup time is also likely to be affected by the version of IE installed, number of plugins it has to load, etc.
Dave - The Developers' Coach