错误“调用的对象已与其客户端断开连接”-使用python和win32com自动化IE 8

2024-06-26 13:43:25 发布

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

我想自动化InternetExplorer8(在Windows7上使用Python2.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。有人知道原因吗?


Tags: inpyimportselfcomclientifline
3条回答

我不能改变我的ie安全设置,但我找到了另一个解决方案,工作在vbscript(不要问我为什么我使用:D)!

http://go-gaga-over-testing.blogspot.co.uk/2013/06/the-object-invoked-has-disconnected.html

  Set ie = WScript.CreateObject("InternetExplorer.Application")

  With ie
       hwnd = .hwnd
       .Navigate theURL
  End With

  Set oShell = CreateObject("Shell.Application")

  For Each Wnd In oShell.Windows
         If hwnd = Wnd.hwnd Then Set ie = Wnd
  Next 

哇哦。我一直在与一个脚本斗争,这个脚本已经工作了3天,试图弄清楚为什么它甚至没有达到第10行。微软一直在我们的组织中自动更新IE到IE10,这给CRM开发人员带来了很大的麻烦。现在我注意到设置已重置为默认值,并且保护模式已打开。

在开发站点时,您可以尝试的最有用的事情之一是推F12并将IE版本设置为其他版本。例如,你的网站曾经在IE9中运行,但在10中中断。这允许您运行IE10并在多个版本中测试代码。我仍然试图找到一种方法,迫使某些网站在特定版本的internet explorer中打开,而不必每次都按F12键。

在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

相关问题 更多 >