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

J Correia correiajREMOVECAPS at hotmail.com
Fri May 20 20:32:05 EDT 2005


<cal_2pac at yahoo.com> wrote in message
news:1116604655.431664.261430 at g49g2000cwa.googlegroups.com...
> Thanks for the response again. The solution is pretty close but not yet
> complete
> This is what I observed.
> a) I tried to use the delay mechanism as suggested below
> ie.
> ie.Navigate('www.google.com')
> while ie.ReadyState !- 4
>        time.sleep(0.5)
>
> d=win32com.client.DispatchWithEvents(ie.Document, Doc_Events)
>
> IE *fails* to load the webpage

Thought this was quite curious so tried it myself (on Python 2.3 Win2k
machine).  Put in some timing conditions and the problem is not that it
fails to load, but that it takes really long (min time during tests = 60
secs
, maximum time 580 secs).

Tried using  just WithEvents, same problem.  The problem seems to
lie with the call to ie.ReadyState while trapping events.  2 things lead me
to believe this...
1) Interrupting the Python code after the browser window opens, results in
the window
 finishing and loading the URL immediately with no problems.
2) Running the code with just Dispatch (no events) and it works fine (< 1
sec).

All I can think is that each call to ie.ReadyState triggers some internal
event which hogs resources to process.

It seems like the problem is with IE Events only... so a possible workaround
(if all you need is the Doc events) is the following:
-------------------------
import win32com.client

class Doc_Events:
    def Ononactivate(self):
        print 'onactivate:', doc.location.href
    def Ononclick(self):
        print 'onclick fired.'
    def Ononreadystatechange(self):
        print 'onreadystatechange:', doc.readyState

ie = win32com.client.Dispatch("InternetExplorer.Application")
ie.Visible = 1
ie.Navigate('http://www.google.com')

while ie.ReadyState != 4:
    time.sleep(1)

doc = ie.Document
doc_events = win32com.client.WithEvents(doc, Doc_Events)
# OR can use following:
# doc = win32com.client.DispatchWithEvents(ie.Document, Doc_Events)

while ie.ReadyState != 4 and doc.readyState != "complete":
    # readystate is case sensitive and differs for ie (R) and doc (r)
    # ie.ReadyState: 0=uninitialised; 1=loading; 2=loaded;
    #                        3=interactive; 4=complete
    time.sleep(1)
-------------------------

HTH,





More information about the Python-list mailing list