views:

90

answers:

1

Hello, actually this is not hang status, i mean..it slow response,
so in that case, i would like to close IE and want to restart from start.
so closing is no problem ,problem is ,how to set timeout ,for example if i set 15sec,
if not webpage open less than 15 sec i want to close it and restart from start.
is this possible to use with IE com interface?
really hard to find solution Paul,

I'm used to follow code to check wether a webpage is completely open or not. But as I mentioned, it is not working well, because IE.navigate is looks like it hangs or does not respond.

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

To avoid blocking problem use IE COM object in a thread.

Here is a simple but powerful example demonstrating how can you use thread and IE com object together. You can improve it for your purpose.

This example starts a thread a uses a queue to communicate with main thread, in main thread user can add urls to queue, and IE thread visits them one by one, after he finishes one url, IE visits next. As IE COM object is being used in a thread you need to call Coinitialize

from threading import Thread
from Queue import Queue
from win32com.client import Dispatch
import pythoncom
import time

class IEThread(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.queue = Queue()

    def run(self):
        ie = None
        # as IE Com object will be used in thread, do CoInitialize
        pythoncom.CoInitialize()
        try:
            ie = Dispatch("InternetExplorer.Application")
            ie.Visible = 1
            while 1:
                url = self.queue.get()
                print "Visiting...",url
                ie.Navigate(url)
                while ie.Busy:
                    time.sleep(0.1)
        except Exception,e:
            print "Error in IEThread:",e

        if ie is not None:
            ie.Quit()


ieThread = IEThread()
ieThread.start()
while 1:
    url = raw_input("enter url to visit:")
    if url == 'q':
        break
    ieThread.queue.put(url)
Anurag Uniyal
hello, thanks for your reply,im using ie com interface because of javascript support.i can't use mechanize with javascript and i was tested other thing.and finally i was figured out best choice for me is PAMIE or ie com interface,can you show me some sample about thread example?if so really helpful thanks
paul
@paul, I have added an example, which should be sufficient for your purpose.
Anurag Uniyal
Hello, im very sorryim quite new to thread,i have no experience it.i was execute script , but continuesly re visited in url's web address. how can i only one time url visit or controlling some method?your script is i think very good,but because of lack of my knowledge :(thanks
paul
please update you question with what exactly you want to do, the script I have given would be more than sufficient for all your use cases
Anurag Uniyal
Hello, i was updated, can i set 15 sec ? so if not webpage can't make complete status, i want to close IE and restart from main(): function.thanks a lot !
paul
thanks a lot :)
paul