javascript execution from Python script

brueckd at tbye.com brueckd at tbye.com
Tue Aug 27 00:39:11 EDT 2002


On Mon, 26 Aug 2002, Justin Guerin wrote:

> I administer a 3rd party software program that has a web based interface.  I 
> want to automate some of the repetitive tasks I find myself doing often, but 
> I can't get directly to the internals, so I figured I could write a script to 
> drive the web based interface.  Many pages contain javascript, and most of 
> the scripting can be done by massaging the javascript source, but as for the 
> rest, I'd rather not write unique code for it.  After all, I can't really 
> control when the interface changes.

Ahh... well, if you're on Windows then you can automate IE using Mark 
Hammond's win32com extensions, and you can even execute Javascript through 
a web browser object.

Here's a little code to help (untested but I've used similar stuff in the 
past):

import win32com
ie = win32com.client.DispatchEx('InternetExplorer.Application.1')
ie.Navigate(someURL)
while ie.Busy:
  time.sleep(0.05)
doc = ie.Document
while not (hasattr(doc, 'readyState') and doc.readyState =='complete'):
  time.sleep(0.05)
pw = doc.parentWindow
pw.execScript("window.alert('foo');") # Your javascript code

Hope this helps,
-Dave





More information about the Python-list mailing list