getting HTML DOM events via win32com
colin roald
colin at moonbase.gungeralv.org
Thu Nov 7 17:40:13 EST 2002
I am trying to puzzle out the win32com interface, specifically as a
means to automate Internet Explorer.
I have been able at various points to get win32com.client.Dispatch
and DispatchWithEvents to work as advertised. My current problem
has to do with connecting to HTML DOM events, ie, through
HTMLDocumentEvents2.
The following code is derived with some modification from a sample
posted by Alex Martelli in December 2000
(http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=utf-8&threadm=91t2mf02dob%40news2.newsguy.com&rnum=1&prev=/groups%3Fq%3DHTMLDocumentEvents2%2Bpython%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3Dutf-8%26selm%3D91t2mf02dob%2540news2.newsguy.com%26rnum%3D1).
It is supposed to trap onclick events from the document. (Code follows.)
On Monday it worked. Today it is not working. I do not know why.
It doesn't throw any errors -- it just doesn't receive any events.
Questions:
(1.) Is there any documentation, anywhere, that explains exactly how
the events describe at
http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/mshtml/reference/events/htmldocumentevents2/htmldocumentevents2.asp
are translated into Python methods? How do I know what name and
signature I'm supposed to give my handler method (Ononclick?)?
What *are* its arguments, anyway?
(2.) Is it possible that I haven't Ensured Module for the right classes?
How would I be able to tell what's missing? (No errors are
thrown.) I am hopelessly confused trying to sort out the functions
of all the Modules and CoClasses and Interfaces and TypeLibs. The
first four lines of EnsureModuleForTypelibOfObject contains an
obscure series of multiple indirections I can't quite puzzle out --
where is this stuff documented?
(3.) gen_py/HTMLDocumentEvents2.py contains an 'onclick' method already,
but it's body ('return self._oleobj_.InvokeTypes(0xfffffda8, LCID,
1, (11, 0), ((9, 1),),pEvtObj)') is too obscure for me to decipher.
what does it do? Am I *supposed* to override it, or supposed to
*not* override it?
Thanks for any help you can give me. Much appreciated!
# ---- begin code ------------------------------------------------------
# testing trapping HTML DOM events
import sys, win32com.client.gencache, pythoncom, win32com.client
import time
from pywin.tools import browser
seen = 0
def EnsureModuleForTypelibOfObject(obj, bForDemand=1):
try:
typeinfo = obj._oleobj_.GetTypeInfo()
clsid = typeinfo.GetTypeAttr()[0]
typelib, index = typeinfo.GetContainingTypeLib()
typelib_attr = typelib.GetLibAttr()
# EnsureModule(typelibCLSID, lcid, major, minor,
progressInstance=None, bValidateFile=1, bForDemand=0,
bBuildHidden=1)
return win32com.client.gencache.EnsureModule(
typelib_attr[0], typelib_attr[1], typelib_attr[3],
typelib_attr[4], bForDemand=bForDemand)
except pythoncom.com_error:
raise TypeError, "This COM object can not automate the
makepy process - please run makepy manually for this object"
ie =
win32com.client.gencache.EnsureDispatch("InternetExplorer.Application")
ie.Navigate('http://www.google.com/')
ie.Visible=1
while not (hasattr(ie, 'Document')): time.sleep(0.1)
html_module = EnsureModuleForTypelibOfObject(ie.Document)
print 'html_module = ',html_module
HTMLDocumentEvents2Class = win32com.client.gencache.GetClassForCLSID(
'{3050F613-98B5-11CF-BB82-00AA00BDCE0B}') # HTMLDocumentEvents2
class Handler(HTMLDocumentEvents2Class):
def __init__(self, obj):
#self.__dict__['seen'] = 0 # evading restrictive
__setattr__
HTMLDocumentEvents2Class.__init__(self, obj)
print 'Handler initialized'
def onclick(self, *args):
print 'onclick!', args
seen +=1
def Onclick(self, *args):
print 'Onclick!', args
seen +=1
def Ononclick(self, eventIDispatch):
print 'Ononclick ',
seen += 1
print seen
# am not sure what this line is supposed to do
eventIDispatch =
win32com.client.gencache.EnsureDispatch(eventIDispatch)
el = eventIDispatch.srcElement
print "el = ", str(el)
print 'clic on %s(%s) "%s", at (%d,%d)' % (
el.tagName, el.id, el.innerText,
eventIDispatch.clientX, eventIDispatch.clientY)
# ------------------------------------------------------------
# properties of event PyIDispatch object: (I think)
# -- all properties of IE4+ DOM 'event' object
# ------------------------------------------------------------
h = Handler(ie.Document)
print 'handler created'
while seen < 5:
pythoncom.PumpWaitingMessages()
ie.Quit()
# ---- begin code ------------------------------------------------------
--
colin | i opened my windows 95 user manual and fell into a magical world.
roald | -- greg kluzak, dishonourable mention, 1998 bulwer-lytton contest
More information about the Python-list
mailing list