[python-win32] Processing IE DOM events.

mferguson at ntlworld.com mferguson at ntlworld.com
Thu Mar 4 12:02:28 EST 2004


> 
> From: <mferguson at ntlworld.com>
> Date: 2004/03/04 Thu AM 10:06:50 GMT
> To: python-win32 at python.org
> Subject: [python-win32] Processing IE DOM events.
> 
> I'm looking the simplest way to handle IE DOM events in my python scripts. This looks promising but obviously isn't quite right:
> 
> >>> doc.getElementById("submit").attachEvent("onClick",myfunc)
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
>   File "<COMObject getElementById>", line 2, in attachEvent
> ValueError: argument is not a COM object
> >>>
> 
> Can this approach be made to work, or is there some other way to achieve the same thing?
> 
> Thanks,
> 
> Mark.

OK, I've managed to get an event handler working, as below:

from win32com.client import gencache, DispatchEx
import time

mod = gencache.EnsureModule('{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}', 0, 1, 1)
docmod = gencache.EnsureModule('{3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}', 0, 4, 0)

class domEvents(docmod.HTMLDocumentEvents2):
    def __init__(self, obj):
        self.seen = 0
        docmod.HTMLDocumentEvents2.__init__(self, obj)

    def Ononclick(self, ev):
        self.seen += 1
        ev = gencache.EnsureDispatch(ev)
        el = ev.srcElement
        print 'click on %s(%s) -  "%s", at (%d,%d)' % (
            el.tagName, el.id, el.innerText,
            ev.clientX, ev.clientY)

           
ie=DispatchEx("InternetExplorer.Application")
ie.Navigate('http://localhost/page.html');
ie.Visible=1

while ie.Busy:
    time.sleep(0.1)

handler = domEvents(ie.Document)
#---------------------------------

And I get event info printed such as:

click on INPUT() -  "", at (361,220)
click on INPUT() -  "", at (518,245)

Now, if I change 'el.id' to 'el.name', it craps out on me thus:

  File "C:\Python23\Lib\site-packages\win32com\server\policy.py", line 275, in _Invoke_
    return self._invoke_(dispid, lcid, wFlags, args)
  File "C:\Python23\Lib\site-packages\win32com\server\policy.py", line 280, in _invoke_
    return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None)
  File "C:\Python23\Lib\site-packages\win32com\server\policy.py", line 541, in _invokeex_
    return func(*args)
  File "D:\Automate\py\dwevents3.py", line 16, in Ononclick
    print 'click on %s(%s) -  "%s", at (%d,%d)' % (
  File "C:\Python23\Lib\site-packages\win32com\client\__init__.py", line 450, in __getattr__
    raise AttributeError, "'%s' object has no attribute '%s'" % (repr(self), attr)
exceptions.AttributeError: '<win32com.gen_py.Microsoft HTML Object Library.IHTMLElement instance at 0x23255840>' object has no attribute 'name'

Next, from the interactive window I do:
>>> e = doc.getElementsByTagName('INPUT')
>>> for i in e:
... 	print i.name
... 	
username
password
submit

What am I misunderstanding here? Why does the element accessed in the event handler have 'tagName' and 'id' attributes but no 'name' attribute?

TIA,

Mark.

-----------------------------------------
Email provided by http://www.ntlhome.com/





More information about the Python-win32 mailing list