python - 错误 "The object invoked has disconnected from its clients"- 使用 python 和 win32com 自动化 IE 8

标签 python scripting ole win32com

我想自动化 Internet Explorer 8(在 Windows 7 上使用 python 2.7)机器。这是我在 a post found on SO 之后的代码:

import sys, time
from win32com.client import WithEvents, Dispatch
import pythoncom
import threading    

stopEvent=threading.Event()

class EventSink(object): 
    def OnNavigateComplete2(self,*args):
        print "complete",args
        stopEvent.set()



def waitUntilReady(ie):
    if ie.ReadyState!=4:
        while 1:
            print "waiting"
            pythoncom.PumpWaitingMessages()
            stopEvent.wait(.2)
            if stopEvent.isSet() or ie.ReadyState==4:
                stopEvent.clear()
                break;   

if __name__ == '__main__':
    time.clock()
    ie=Dispatch('InternetExplorer.Application',EventSink)
    ev=WithEvents(ie,EventSink)       
    ie.Visible=True
    ie.AddressBar = True
    ie.Navigate("http://www.sap.com/austria/index.epx")
    waitUntilReady(ie)

我收到以下 http://www.sap.com/austria/index.epx 错误消息:

waiting
waiting
Traceback (most recent call last):
  File "C:\Users\w\My Documents\Aptana Studio 3 Workspace\MyApp\src\GoToIdeaWindow.py", line 41, in <module>
    waitUntilReady(ie)
  File "C:\Users\w\My Documents\Aptana Studio 3 Workspace\MyApp\src\GoToIdeaWindow.py", line 26, in waitUntilReady
    if stopEvent.isSet() or ie.ReadyState==4:
  File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 463, in __getattr__
    return self._ApplyTypes_(*args)
  File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 456, in _ApplyTypes_
    self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
pywintypes.com_error: (-2147417848, 'The object invoked has disconnected from its clients.', None, None)

该代码非常适用于 google.com 或 bbc.com。有谁知道可能是什么原因?

最佳答案

在 IE9 上,您需要降低安全设置才能使脚本运行:

IE9 -> Internet Options -> Security -> Trusted Sites    : Low
IE9 -> Internet Options -> Security -> Internet         : Medium + unchecked Enable Protected Mode
IE9 -> Internet Options -> Security -> Restricted Sites : unchecked Enable Protected Mode

关于python - 错误 "The object invoked has disconnected from its clients"- 使用 python 和 win32com 自动化 IE 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6661005/

相关文章:

python - 创建 exe 时,cx_Freeze 在我的项目中找不到自定义模块

python - NSP 中最小化的绝对差

powershell - 如何在Powershell中获取Parent的父目录?

linux - Bash 中的延迟监控

bash - 通过 Bash 传递命令行参数

internet-explorer - 我的 ActiveX 控件如何在 Internet Explorer 中接管打印、另存为、查找等?

c - 如何使用 DispCallFunc 在纯 C 中调用 IStream::SetSize(0)

Delphi 7 和 Excel 2007 打开文件错误

python - 捕获错误并退出

python - 将 pandas 时间序列加入日期范围