[python-win32] Problem Controlling Internet Explorer

Rick Kaye rick@kaye.to
Thu, 06 Mar 2003 18:14:23 -0500


I am trying to control Internet Explorer from a Python script and running
into a problem. My script looks like this:

from win32com.client import Dispatch

IE=Dispatch("InternetExplorer.Application")
while IE.Busy:
        pass

IE.Visible=1

IE.Navigate("c:\\test.html")
while IE.Busy:
        pass

page=IE.Document
links=page.links
links[0].click()



If test.html looks like this:

<html>
    <body>
        <script language="Javascript">
            function myalert () 
                {
                    alert("Don't click so hard");
                }
        </script>
    This link should pop up an alert box and it does from python as
    well as when clicked with the mouse
    <a href="Javascript:myalert()"> Click ME</a>
    </body>
</html>

the script works fine. It starts Internet Explorer and pops up the alert
box.

But if test.html looks like this:

<html>
    <body>
        <script language="Javascript">
            function myalert () 
                {
                    window.open("/test2.html");
                }
        </script>
    This link should open a new window with the test2 document. It      
    works when clicked with the mouse but not when controlled from      
    Python.
    <a href="Javascript:myalert()"> Click ME</a>
    </body>
</html>

Internet Explorer opens and plays the sound effect as if the link was
clicked but no window opens. If I use Python to open this html page and then
click the link with the mouse, it works fine.

While this example illustrates the problem, in my real application, the html
page that contains the link I'm trying to click is not under my control. So
I can't change that page. Can anyone give me some help?