Invoking JavaScript event handler in Internet Explorer by python/win32com

R.Marquez ny_r_marquez at yahoo.com
Mon Mar 3 15:21:33 CET 2003


fredegar at haftmann-online.de (Florian Fredegar Haftmann) wrote in message news:<bb76ef4c.0303030349.7d31bb04 at posting.google.com>...
> Hi!
> 
> I have the following problem:
> I'm programming a python programme which controls Internet Explorer
> via ActiveX/COM. When a document is read, an event handler is
> associated with each button; each time the button is clicked, a
> certain action is performed.
> Since there could also be a "native" JavaScript event handler, it is
> disabled.
> When my own handler is finished, he is supposed to call the old
> "native" handler. But how is that to be done?
> 
> If the old event handler is stored in self.oldevt, a call
> self.oldevt()
> only returns a textual representation; i've tried also calls like
> .Fire() and .Invoke(), but self.oldevt does'nt have such attributes.
> 
> Makepy does not work on self.oldevt.
> 
> Of course you could use the .click method, but since I'd like to do
> the same procedure with other event handlers, too, that is'nt useful.
> 
> Any hints how to call the JavaScript event handler from python?
> 
> Code fragment:
> 
> [...]
> 
> class ButtonHandler(html.HTMLButtonElementEvents):
> 
>     def __init__(self, button, oldevt = None):
> 
>         self.button = button
>         self.oldevt = oldevt
>         html.HTMLButtonElementEvents.__init__(self, button)
> 
>     def Ononclick(self):
> 
>         [... any action ...]
>         if self.oldevt:
>             self.oldevt() <----------------------- ?????????????????
> 
> tagset = doc.getElementsByTagName('input')
> for idx in xrange(tagset.length):
>     tag = tagset(idx)
>     if tag.type.lower() in ('submit', 'button', 'reset'):
>         oldevt = getattr(tag, 'onclick', None)
>         bhandler = ButtonHandler(tag, oldevt)
>         if oldevt:
>             tag.onclick = u""
> 
> 
> Thanks for any remarks
> Florian

Not sure, but I think that you can invoke it by the index of the
button:

class ButtonHandler(html.HTMLButtonElementEvents):

    def __init__(self, button, oldevt = None, idx):
        self.idx = idx

    def Ononclick(self):

        [... any action ...]
        if self.oldevt:
            doc.[self.idx].click()

If not, try finding out a way to do this.  I us this code to click on
the third button on the first form of a page:

    doc.forms[0][3].click()




More information about the Python-list mailing list