2011-07-12 60 views
9

我想使Internet Explorer 8(在Windows 7上使用Python 2.7)自动化。这里是a post found on SO后我的代码:错误“调用的对象与客户端断开连接” - 使用python和win32com自动化IE 8

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。有人知道可能是什么原因吗?

+0

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

回答

10

在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 
+0

谢谢。我在IE-11上遇到了这个问题已经有两天了,这个简单的设置解决了它! – user2979010

2

哇。我一直在努力研究一个为期三天的剧本,试图找出它甚至没有达到第十行的原因。微软一直在IE10中自动更新Internet Explorer,这给CRM开发者带来了很大的麻烦。我现在注意到设置已重置为默认设置,并且保护模式已打开。

您可以在开发您的网站时尝试的最有用的事情之一是推送F12并将IE版本设置为其他版本。例如,您的网站曾经在IE9中工作,但是分成了10.这允许您运行IE10并以多个版本测试您的代码。我仍然试图找到一种方法来强制某些网站在特定版本的Internet Explorer中打开,而无需每次都推送F12。

+1

你可以告诉IE使用一个这个标签的特定版本: alanaktion

0

我有几分类似的问题,我所做的是(但它是用C#.NET MSHTML和SHDOCVW):

  • 降低了安全性(在上网选择安全选项卡)在Internet Explorer中的水平(就像你@Skarab曾试图这样做),
  • 初始化Internet Explorer的变量像一个空值:

    /*INITIALIZE THE BROWSER VARIABLE TO NULL VALUE*/ 
    SHDocVw.InternetExplorer ie =null; 
    ie = new SHDocVw.InternetExplorer(); 
    

希望这可以帮助...

相关问题