Python:检查 IE 状态时出错

发布于 2024-08-15 21:01:44 字数 574 浏览 8 评论 0原文

请帮助我使用 Python 2.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)
..........................

但是当我将 url 更改为“yahoo.com”时 - 没有错误。
检查 ReadyState 的结果如何可能依赖于 url?

Please, help me with Python 2.6 and win32com.

I'm a newbie to Python and I got error
when I start the next program:

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

But when I change url to, for example, 'yahoo.com' -
there are no errors.
How can result of checking ReadyState may be dependant on url??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咿呀咿呀哟 2024-08-22 21:01:44

睡眠技巧不适用于 IE。实际上,您需要在等待时发送消息。顺便说一句,我认为线程不会起作用,因为 IE 讨厌不在 GUI 线程中。

这是一个基于 ctypes 的消息泵,通过它我能够获得 4 ReadyState对于“hotfile.com”和“yahoo.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

The sleep trick won't work with IE. You actually need to pump messages while you wait. I don't think a thread will work, by the way, because IE hates to not be in the GUI thread.

Here's a ctypes-based message pump, with which I was able to get a 4 ReadyState for "hotfile.com" and "yahoo.com". It pulls all the messages currently on the queue, and processes them before running the check.

(Yes, this is pretty hairy, but you can tuck this away into a "pump_messages" function so you at least don't have to look at it!)

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文