2011-07-12 61 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(); 
    

希望這可以幫助...

相關問題