executing JavaScript from Python

Geoff Talvola gtalvola at nameconnector.com
Thu Feb 1 19:14:01 EST 2001


(on the subject of getting web page contents _after_ the JavaScript has been executed, using Internet Explorer on Windows:)

I fiddled with this a bit more, and here's a class that actually waits for IE to be done before trying to get the HTML, and which lets you refresh to make sure you're not getting a cached page.  Note that this only works for GET operations -- I couldn't figure out the magic incantation to get POST to work.  (Navigate has a PostData argument, but whatever I put there, it just seems to ignore.)

I'm not going to spend any more time on this, but if someone can figure out how to get POST to work, I'm sure it would be appreciated...

############
import time
from win32com.client.gencache import EnsureDispatch
from win32com.client import constants

class InternetExplorerGetter:
    def __init__(self):
        self.ie = EnsureDispatch('InternetExplorer.Application')

    def get(self, url):
        self.ie.Navigate(url)
        return self.waitForResult()

    def refresh(self):
        self.ie.Refresh()
        return self.waitForResult()

    def waitForResult(self):
        while 1:
            state = self.ie.ReadyState
            if state == constants.READYSTATE_COMPLETE: break
            time.sleep(0.1)
        return self.ie.Document.documentElement.innerHTML

if __name__ == '__main__':
    print InternetExplorerGetter().get('http://www.python.org')
############

--


- Geoff Talvola
  Parlance Corporation
  gtalvola at NameConnector.com





More information about the Python-list mailing list