executing JavaScript from Python

Geoff Talvola gtalvola at nameconnector.com
Thu Feb 8 14:04:26 EST 2001


Hamish Lawson wrote:

> Geoff Talvola <gtalvola at NameConnector.com> wrote:
>
> > On Windows, you can use Internet Explorer via COM:
> >
> > >>> from win32com.client.gencache import EnsureDispatch
> > >>> ie = EnsureDispatch('InternetExplorer.Application')
> > >>> ie.Navigate('http://localhost/javascript.html')
> > >>> print ie.Document.documentElement.innerHTML
> > <HEAD></HEAD>
> > <BODY>
> > <SCRIPT>
> > document.write('Hello, World!')
> > </SCRIPT>
> > Hello, World! </BODY>
> > >>>
>
> I tried that out myself and got the error below. I'm using Python 2.0
> with win32all build 135.
>
> Traceback (most recent call last):
>   File "C:\temp\test2.py", line 11, in ?
>     print ie.Document.documentElement.innerHTML
>   File "C:\Python\win32com\client\__init__.py", line 340, in __getattr__
>     return apply(self._ApplyTypes_, args)
>   File "C:\Python\win32com\client\__init__.py", line 334, in
> _ApplyTypes_
>     return self._get_good_object_(apply(self._oleobj_.InvokeTypes,
> (dispid, 0, wFlags, retType, argTypes) + args), user, resultCLSID)
> pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None,
> None, None, 0, -2147467259), None)
>
> Hamish Lawson

You have to wait until the document is done loading before you can access the innerHTML -- if you try to access innerHTML too quickly you'll get the exception.  I sent out a followup message with code that does the waiting; if you can't find the message, let me know and I'll email it to you.

--


- Geoff Talvola
  Parlance Corporation
  gtalvola at NameConnector.com





More information about the Python-list mailing list