Discussion:
javascript execution from Python script
Gerhard Häring
2002-08-26 17:32:28 UTC
Permalink
Hello everyone,
Does anyone know if there is a way to execute javascript from within a Python
script?
I'm pretty sure there is none. Your best bet is to expose an existing
JavaScript implementation to Python.
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.
Jython can call to Java code, too: they're both executed in the same
Virtual Machine.
And actually, since I'm more interested in javascript, I'm not sure it
matters, anyway.
It probably doesn't. The only connection is that many browsers have
techniques to script Java applets via JavaScript. Sometimes, it also
works the other way round, I hear. So you could _perhaps_ write a Jython
applet that has access to the JavaScript engine in the same browser. If
I were a project manager, I'd say RISK FACTOR: HIGH.
(I'm afraid I'm quite unfamiliar with Java and javascript.)
"Java is not JavaScript", that's even the subtitle of the
de.comp.lang.java FAQ ;-) All they share are the first four letters.

Gerhard
--
mail: gerhard <at> bigfoot <dot> de registered Linux user #64239
web: http://www.cs.fhm.edu/~ifw00065/ OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9 3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))
Guyon Morée
2002-08-26 17:35:01 UTC
Permalink
Hi Justin,

I think you're missing the picture here. Don't mix-up Javascript with Java.
Javascript is 'embedded' within your HTML and thus interpreted by your
browser. I guess browser-stuff is the only use for Javascript anyway. So
either you have to do some more research into what you are really trying to
accomplish or you need to look into Python CGI, where you can mix the
Javascript into your output HTML.

I hope to have helped you,
Guyon


"Justin Guerin" <jguerin at cso.atmel.com> wrote in message
news:mailman.1030382618.2061.python-list at python.org...
Matt Gerrans
2002-08-26 18:10:50 UTC
Permalink
Java and JavaScript are entirely different and unrelated animals; the fact
that they have similar names is just the result of a horrible marketing
blunder. Jython is very nifty and allows you to call Java from Python and
vice versa (in fact, regarding the eternal question of Tkinter vs. wxWindows
et al, I am beginning to like the idea of using Swing via Jython), but from
what you've said it looks like you are really more interested in JavaScript,
not Java.

What exactly are you trying to accomplish?

Also, what platform are you using? If it is Windows, then there may be
hope of solving your problem using the Windows Script Host; Python works
pretty well with it. Python can also be used to automate Internet Explorer
on the Windows platform (I wonder if that can be done on the Mac platform as
well?). If it is Unix/Linux, I'm not sure what options you have.
Paul Rubin
2002-08-27 14:29:57 UTC
Permalink
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.
Really, dealing with interface changes will need your attention whether
or not your program can interpret js.

Generally the simplest approach is for you to examine the JS and figure
out what it's doing, and program your script to act accordingly. For
example, if the JS is highlighting buttons on rollover, ignore that.
Other typical JS runs on the onclick event of a form button. It
typically validates the form content and then runs form.submit().
So just make your script post the form submit directly. Etc.

That said, I do think a JS to Python converter would be a worthwhile
thing for someone to write someday. It wouldn't be trivial but it
wouldn't be terribly hard.
Cameron Laird
2002-08-27 13:16:15 UTC
Permalink
In article <mailman.1030391201.29388.python-list at python.org>,
Post by Matt Gerrans
Java and JavaScript are entirely different and unrelated animals; the fact
that they have similar names is just the result of a horrible marketing
blunder.
Only programmers regard it as a blunder.
.
.
.
I had no idea they were THAT unrelated.
They share some syntax, including keywords.
That was done mostly for intellectually
illegitimate reasons.
Post by Matt Gerrans
Jython is very nifty and allows you to call Java from Python and
vice versa (in fact, regarding the eternal question of Tkinter vs.
wxWindows et al, I am beginning to like the idea of using Swing via
Jython), but from what you've said it looks like you are really more
interested in JavaScript, not Java.
What exactly are you trying to accomplish?
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.
Ugh.

I applaud your instincts: automate those repetitive
tasks.

Key facts: while it's feasible to write a JavaScript
interpreter in Python, no one's done so, to my knowledge.
You're absolutely right that your vendor could trick up
the page at any time. This makes it sound rather hopeless.

It's actually both better and worse than this.
JavaScript adds nothing, in a dataflow sense; if you
read the page carefully, you'll find that you can get
at all the information you need without JavaScript.
It's fairly common, in fact, for the visual display
to suffer profound changes, with much different
JavaScript coding, while the underlying content, which
is what you're after, remains invariant.

With one exception: the one serious problem JS intro-
duces is that it can invoke Java code, generally to
"call home" through a network connection. If that's
happening, you've got a serious challenge ahead of you.
The good news, in that case, is that a JS interpreter
wouldn't help you anyway; you'd need Java as well.

I do this kind of Web scraping occasionally. I
generally find it worthwhile, by hand; the benefits of
the automation are worth the cost of deciphering the
JavaScript "by hand".

There are also commercial JavaScript interpretive
engines available, but, unless you have a serious
budget, I recommend against them.
.
.
.
--
Cameron Laird <Cameron at Lairds.com>
Business: http://www.Phaseit.net
Personal: http://starbase.neosoft.com/~claird/home.html
Justin Guerin
2002-08-26 19:45:22 UTC
Permalink
Post by Matt Gerrans
Java and JavaScript are entirely different and unrelated animals; the fact
that they have similar names is just the result of a horrible marketing
blunder.
I had no idea they were THAT unrelated.
Post by Matt Gerrans
Jython is very nifty and allows you to call Java from Python and
vice versa (in fact, regarding the eternal question of Tkinter vs.
wxWindows et al, I am beginning to like the idea of using Swing via
Jython), but from what you've said it looks like you are really more
interested in JavaScript, not Java.
What exactly are you trying to accomplish?
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.
Post by Matt Gerrans
Also, what platform are you using? If it is Windows, then there may be
hope of solving your problem using the Windows Script Host; Python works
pretty well with it. Python can also be used to automate Internet
Explorer on the Windows platform (I wonder if that can be done on the Mac
platform as well?). If it is Unix/Linux, I'm not sure what options you
have.
I'm using Linux to develop, but right now, our server is on NT. I could run
the script from my desktop under Linux, but I think I'd lose the ability to
reboot the server, should that become necessary. Also, it is possible that
our server will move to Unix at some time, so a maximum of portability is
important.

Thanks to Gerhard, Guyon, Paul and Matt for your insights and assistance.
It's good to know I'm not just missing something.

Justin

brueckd
2002-08-27 22:55:56 UTC
Permalink
That works well -- you might also want to throw in an "ie.visible=1" so you
can see (and dismiss) it.
Ah, yes, thanks for noticing that. So I went and dug up some code and
noticed one other thing that is useful during testing and that's a way to
bring the window to the foreground (because for me it usually opens up
behind other windows). There's probably a good and proper way to do this,
but this horrible hack works too:

ie.Navigate('about:blank')
while ie.Busy, etc... # wait for doc to load
ie.Visible = 1
hwnd = win32gui.FindWindow(None, 'about:blank - Microsoft Internet
Explorer')
if hwnd:
win32gui.SetForegroundWindow(hwnd)
By the way (this should probably be a different thread), is this some
No, more an idiosyncrasy of my code-from-memory. ;-)
import win32com
ie = win32com.client.DispatchEx('InternetExplorer.Application.1')
File "<pyshell#1>", line 1, in ?
ie = win32com.client.DispatchEx('InternetExplorer.Application.1')
AttributeError: 'module' object has no attribute 'client'
import win32com.client
ie = win32com.client.DispatchEx('InternetExplorer.Application.1')
Why is it necessary to import win32com.client explicitly in this case?
win32com is just a package, so importing it binds the win32com variable to
stuff in that package's __init__.py (e.g. in this case that includes the
SetupEnvironment function). client is also a package, and inside its
__init__.py is DispatchEx.

-Dave
Paul Rubin
2002-08-26 17:45:57 UTC
Permalink
Java and javascript aren't really related, any more than "car" and "carpet".

Javascripts semantics are sort of a subset of Python's, so compiling
javascript to Python bytecode is a plausible idea that I've thought
about at times. It would be useful for a Grail-like browser written
in Python. I don't know of anyone ever trying to implement it though.

Yes, jython can call java code from python scripts.
Matt Gerrans
2002-08-27 17:37:57 UTC
Permalink
That works well -- you might also want to throw in an "ie.visible=1" so you
can see (and dismiss) it.

By the way (this should probably be a different thread), is this some
import win32com
ie = win32com.client.DispatchEx('InternetExplorer.Application.1')
Traceback (most recent call last):
File "<pyshell#1>", line 1, in ?
ie = win32com.client.DispatchEx('InternetExplorer.Application.1')
AttributeError: 'module' object has no attribute 'client'
import win32com.client
ie = win32com.client.DispatchEx('InternetExplorer.Application.1')
Why is it necessary to import win32com.client explicitly in this case?


<brueckd at tbye.com> wrote in message
Here's a little code to help (untested but I've used similar stuff in the
import win32com
ie = win32com.client.DispatchEx('InternetExplorer.Application.1')
ie.Navigate(someURL)
time.sleep(0.05)
doc = ie.Document
time.sleep(0.05)
pw = doc.parentWindow
pw.execScript("window.alert('foo');") # Your javascript code
Hope this helps,
-Dave
Matt Gerrans
2002-08-28 16:15:21 UTC
Permalink
Okay. Based on the code Dave posted, I thought he was able to do the
import of win32com and then use win32com.client.DispatchEx() (like importing
os and using os.path.abspath() and whatnot). Based on his subsequent post,
that doesn't seem to be the case, though. From this, I infer that os
simply imports path and that is why you can use path members (from os)
without explicitly importing it... okay I just went and looked and that is
what it does (actually, it picks the appropriate path module depending on
the platform as assigns it to os.path).

"Mark Hammond" <mhammond at skippinet.com.au> wrote in message
The function you are trying to use is a member of the win32com.client
module, not directly in win32com. The implementation details of
Python's package import mechanism means that this is not *always*
necessary, but it is always "right".
Mark.
Mark Hammond
2002-08-28 01:27:24 UTC
Permalink
By the way (this should probably be a different thread), is this some
import win32com
ie = win32com.client.DispatchEx('InternetExplorer.Application.1')
File "<pyshell#1>", line 1, in ?
ie = win32com.client.DispatchEx('InternetExplorer.Application.1')
AttributeError: 'module' object has no attribute 'client'
import win32com.client
ie = win32com.client.DispatchEx('InternetExplorer.Application.1')
Why is it necessary to import win32com.client explicitly in this case?
The function you are trying to use is a member of the win32com.client
module, not directly in win32com. The implementation details of
Python's package import mechanism means that this is not *always*
necessary, but it is always "right".

Mark.
brueckd
2002-08-27 04:39:11 UTC
Permalink
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
Peter Hansen
2002-08-27 12:10:17 UTC
Permalink
Regards, Bogdan
Any code of your own that you haven't looked at for six or more
months, might as well have been written by someone else. (Eagleson
is an optimist, the real number is more like three weeks.)
Eagleson was also not writing unit tests for his code. ;-)

-Peter
Justin Guerin
2002-08-26 17:19:24 UTC
Permalink
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
Bo M. Maryniuck
2002-08-27 07:45:00 UTC
Permalink
I checked http://www.python.org/doc/essays/comparisons.html, where Guido
wrote that "a Python implementation written in Java is under development,
JavaScript is NOT Java.
--
Regards, Bogdan

Eagleson's Law:
Any code of your own that you haven't looked at for six or more
months, might as well have been written by someone else. (Eagleson
is an optimist, the real number is more like three weeks.)
Mark Hammond
2002-08-27 00:53:35 UTC
Permalink
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.
Continue reading on narkive:
Loading...