javascript execution from Python script

Mark Hammond mhammond at skippinet.com.au
Tue Aug 27 02:53:35 CEST 2002


Justin Guerin wrote:
> Hello everyone,
> 	Does anyone know if there is a way to execute javascript from within a Python 
> script?  I'm trying to write a script to browse a web site that makes use of 
> javascript rather liberally.
> 	I checked http://www.python.org/doc/essays/comparisons.html, where Guido 
> wrote that "a Python implementation written in Java is under development, 
> which allows calling Python code from Java and vice versa."  If he's 
> referring to Jython, then maybe I need to check the Jython web site more 
> carefully, but from what I saw, it allows you to run Python code from within 
> Java, but not Java code from within Python.  And actually, since I'm more 
> interested in javascript, I'm not sure it matters, anyway.  (I'm afraid I'm 
> quite unfamiliar with Java and javascript.)
> 	Google hasn't helped me thus far, but maybe I've got the wrong search terms.  
> "javascript execution python" doesn't seem to get me what I'm looking for.
> 	Alternately, is there a library to parse javascript into statements that can 
> be exec()'d in Python?  That would probably be good enough.
> 	Any help / pointers are greatly appreciated.  Thanks in advance!
> Justin Guerin

Ignoring the "java != javascript" to the existing followups...

Unfortunately, the problem is bigger than simply javascript - using the 
ActiveScripting tools, I can execute arbitary javascript (or vbscript) 
code from within Python.

However, the bigger problem is the object model.  javascript inside the 
browser implements the "document", "window" and other objects - these 
are not part of javascript itself, but part of the object model exposed 
to javascript by the application.

Your best choice may simply be to automate IE via Python - don't use the 
HTML directly using Python modules, but instead use win32com to get IE 
to open the page, and use the object model to navigate to whatever it is 
you are after.

Hope this helps,

Mark.




More information about the Python-list mailing list