Python:检查IE时出错

2024-03-29 10:43:34 发布

您现在位置:Python中文网/ 问答频道 /正文

请帮助我使用python2.6和win32com。在

我是Python的新手,但我出错了 当我开始下一个程序时:

import pywintypes
from win32com.client import Dispatch
from time import sleep

ie = Dispatch("InternetExplorer.Application")
ie.visible=1
url='hotfile.com'

ie.navigate(url)
while ie.ReadyState !=4:
    sleep(1)
print 'OK'
..........................
Error message:
 while ie.ReadyState !=4:
 ...

pywintypes.com_error: 
(-2147023179, 'Unknown interface.', None, None)
..........................

但是当我把网址改为雅虎' - 没有错误。
检查ReadyState的结果怎么可能取决于url??在


Tags: fromimport程序comclientnoneurlsleep
1条回答
网友
1楼 · 发布于 2024-03-29 10:43:34

睡眠诀窍对IE不起作用。你实际上需要在等待时输入消息。顺便说一句,我不认为线程可以工作,因为IE不喜欢不在GUI线程中。在

这是一个基于ctypes的消息泵,通过它,我可以为hotfile.com网站“和”雅虎". 它提取队列中当前的所有消息,并在运行检查之前处理它们。在

(是的,这是相当棘手的,但你可以把它塞进“pump_messages”函数中,这样你至少不必看它!)在

from ctypes import Structure, pointer, windll
from ctypes import c_int, c_long, c_uint
import win32con
import pywintypes
from win32com.client import Dispatch

class POINT(Structure):
    _fields_ = [('x', c_long),
                ('y', c_long)]
    def __init__( self, x=0, y=0 ):
        self.x = x
        self.y = y

class MSG(Structure):
    _fields_ = [('hwnd', c_int),
                ('message', c_uint),
                ('wParam', c_int),
                ('lParam', c_int),
                ('time', c_int),
                ('pt', POINT)]

msg = MSG()
pMsg = pointer(msg)
NULL = c_int(win32con.NULL)

ie = Dispatch("InternetExplorer.Application")
ie.visible=1
url='hotfile.com'
ie.navigate(url)

while True:

    while windll.user32.PeekMessageW( pMsg, NULL, 0, 0, win32con.PM_REMOVE) != 0:
        windll.user32.TranslateMessage(pMsg)
        windll.user32.DispatchMessageW(pMsg)

    if ie.ReadyState == 4:
        print "Gotcha!"
        break

相关问题 更多 >