开发者

How to close not responsive Win32 Internet Explorer COM interface?

开发者 https://www.devze.com 2022-12-11 02:11 出处:网络
actually this is not hang status, i mean..it slow response, so in that case, i would like to close IE and

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 wit开发者_StackOverflowh 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)


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号