How to receive events (eg. user mouse clicks) from IE

Roger Upole rupole at hotmail.com
Thu May 19 18:05:09 EDT 2005


Usually you get that error if you try to access the Document object
before the page has loaded.  Try adding a delay after ie.Navigate,
something like

while ie.ReadyState<>4:
    time.sleep(0.5)
There are some constants that show up in win32com.client.constants
that represent the ReadyState's, but I can't remember the names offhand.

The only real reference for the Html objects (other than reading the
generated module) is MSDN.  Google turned this up:
http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/mshtml/reference/events/htmlanchorevents/htmlanchorevents.asp

           Roger


<cal_2pac at yahoo.com> wrote in message 
news:1116537405.495521.252570 at f14g2000cwb.googlegroups.com...
> Thanks for your prompt responses and the code.
> However, when I run the code I get com error
> d=win32com.client.DispatchWithEvents(ie.Document, Doc_Events)
>  File "C:\Python23\lib\site-packages\win32com\client\__init__.py",
> line 199, in __getattr__
>    return getattr(self._obj_, attr)
>  File "C:\Python23\lib\site-packages\win32com\client\__init__.py",
> line 455, in __getattr__
>    return self._ApplyTypes_(*args)
>  File "C:\Python23\lib\site-packages\win32com\client\__init__.py",
> line 446, in _ApplyTypes_
>    return self._get_good_object_(
> com_error: (-2147352567, 'Exception occurred.', (0, None, None, None,
> 0, -2147467259), None)
>
> I am a newbie to python(started coding a few months ago).
> This appears to be a fairly common error when COM objects are not
> hooked up properly. Though I have not been able to find a solution or
> the reason for this.
> Thanks again for the explanation.All the information that I had
> acquired now seems to fall into place. I was not aware about
> doc_mod.HTMLAnchorEvents. Where can I find more documentation about
> this?
>
> Roger Upole wrote:
>> Here's a few more lines that hook the document's onactivate event.
>>
>> import win32com.client
>>
>>
> ie_mod=win32com.client.gencache.EnsureModule('{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}'
>
>> ,0, 1, 1)
>>
> doc_mod=win32com.client.gencache.EnsureModule('{3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}'
>
>> ,0 ,4, 0)
>> class IE_Events(ie_mod.DWebBrowserEvents2):
>>     def OnNavigateComplete2(self, pDisp, URL):
>>         print 'OnNavigateComplete2:', URL
>>
>> class Doc_Events(doc_mod.HTMLAnchorEvents):
>>     def Ononactivate(self):
>>         print 'onactivate', self.activeElement.outerHTML
>>
>>
> ie=win32com.client.DispatchWithEvents('InternetExplorer.Application',IE_Events)
>> ie.Visible=1
>> ie.Navigate('http://www.google.com')
>>
>> d=win32com.client.DispatchWithEvents(ie.Document, Doc_Events)
>>
>>     Roger
>>
>>
>> <cal_2pac at yahoo.com> wrote in message
>> news:1116529345.146032.91880 at f14g2000cwb.googlegroups.com...
>> > Hi
>> > Thanks for the response and for the code.
>> > However, I want to trap events like mouse click on the HTML
> document
>> > loaded by the web browser control. The code mentioned below
> provides
>> > events from the web browser control. I need to find out on which
>> > particular HTML tag did the user click for example.
>> > How do I find that out? There should be some way to refer to a
> document
>> > from a given web browser control and start receiving events from it
>> >
>> >
>> > Roger Upole wrote:
>> >> The two you'll need to run makepy for are Microsoft Internet
> Controls
>> > and
>> >> Microsoft HTML object Library.  If you run them manually, you
> should
>> > be
>> >> able to look at the generated code to get the guids.
>> >> Here's a minimal example:
>> >>
>> >> import win32com.client
>> >>
>> >>
>> >
> ie_mod=win32com.client.gencache.EnsureModule('{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}',0,
>> >
>> >> 1, 1)
>> >> class IE_Events(ie_mod.DWebBrowserEvents2):
>> >>     def OnNavigateComplete2(self, pDisp, URL):
>> >>         print 'OnNavigateComplete2:', URL
>> >>
>> >>
>> >
> ie=win32com.client.DispatchWithEvents('InternetExplorer.Application',IE_Events)
>> >> ie.Visible=1
>> >> ie.Navigate('http://www.google.com')
>> >>
>> >>    hth
>> >>       Roger
>> >>
>> >>
>> >> <cal_2pac at yahoo.com> wrote in message
>> >> news:1116476411.853695.235670 at g14g2000cwa.googlegroups.com...
>> >> >I am trying to trap events from internet explorer eg. when user
>> > clicks
>> >> > on an html link - I need to get notified for that event.
>> >> >
>> >> > After looking through the newgroups / internet and reading
> through
>> >> > various sections in programming python on win32 - I understand
> that
>> >> > this can be done using DispatchWithEvents.
>> >> > I have also referred to discussions on comp.lang.python
>> >> >
>> >
> http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/a3c502d06412a5f8/0ee3083e71316da7?q=dispatchwithevents&rnum=43#0ee3083e71316da7
>> >> >
>> >> > and
>> >> >
>> >> >
>> >
> http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/1da0668794851296/5bcec1fda216c598?q=dispatchwithevents&rnum=19#5bcec1fda216c598
>> >> >
>> >> > So far - none of the newgroups postings report that IE events
> were
>> >> > trapped successfully (at least I could not find any). However,
>> > there is
>> >> > enough evidence that it can be done.
>> >> > My problems at the moment:
>> >> > a) The examples use early binding. However, I cannot get Python
>> > makepy
>> >> > to generate the code to force makepy process at run time - since
>> > COM
>> >> > makepy utility that I invoke from python win 32 does not have
> any
>> > entry
>> >> > for internet explorer.
>> >> > I tried to find the CLSID for IE
>> > 3050F613-98B5-11CF-BB82-00AA00BDCE0B
>> >> > but I get an exception
>> >> > b) Also one of the examples suggest that following code should
> work
>> >> >
>> >> >> mod = EnsureModule(...)
>> >> >
>> >> >> class MyEvents(mod.IDocumentEvents):
>> >> >>    # your methods here....
>> >> >
>> >> >> handler = MyEvents(ie.document)
>> >> >
>> >> >> # handler should start recieving events.
>> >> >
>> >> > however, what CLSID is to be used in EnsureModule... . I tried
> with
>> > a
>> >> > few but I always get the error 'NoneType' object has no
> attribute
>> >> > 'IDocumentEvents'
>> >> >
>> >> >
>> >> > An example that 'works' will be very useful
>> >> >
>> >>
>> >>
>> >>
>> >> ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure
> Usenet
>> > News==----
>> >> http://www.newsfeeds.com The #1 Newsgroup Service in the World!
>> > 120,000+ Newsgroups
>> >> ----= East and West-Coast Server Farms - Total Privacy via
> Encryption
>> > =----
>> >
>>
>>
>>
>>
>> ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet
> News==----
>> http://www.newsfeeds.com The #1 Newsgroup Service in the World!
>>100,000 Newsgroups
>> ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
> 



----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----



More information about the Python-list mailing list