#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    40
    Rep Power
    12

    Can't get to Internet Explorer COM object


    Hi all

    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:

    Traceback (most recent call last):
    File "apptester.py", line 278, in ?
    t.makeGoldenFiles()
    File "apptester.py", line 152, in makeGoldenFiles
    self.makeGoldenFile( app )
    File "apptester.py", line 97, in makeGoldenFile
    ie.Visible = 1
    File "C:\Python22\Lib\site-packages\win32com\client\dynamic.py", line 521, in __setattr__
    raise AttributeError, "Property '%s.%s' can not be set." % (self._username_,
    attr)
    AttributeError: Property 'InternetExplorer.Application.Visible' can not be set.
    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:

    Code:
        def makeGoldenFile( self, app ):
            global g_goldenFilesTimeout
            ie = win32com.client.Dispatch( "InternetExplorer.Application" )
            time.sleep( 2 )
            ie.Visible = 1
            ie.FullScreen = 1
            ie.Navigate( app.url )
            time.sleep( g_goldenFilesTimeout )
            # Screenshot
            fn = app.getGoldenFilename()
            cmd = "c:\\PROGRA~1\\CAP\\cap.exe /full /file=" + fn
            os.system( cmd )
            ie.Quit()
            self.addIgnoreAreas( fn, app )
            return self.compareScreenshots( fn, app.getGoldenFilename() )
    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).

    Any ideas?

    -Antun
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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:

    Code:
    while 1:
        try:
            ie.Visible = 1
            break
        except AttributeError:
            sleep(1)
    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.

    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.

    Regards,

    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo