错误“被调用的对象已经从它的客户端断开连接”——使用python和win32com自动运行IE 8

时间:2022-06-05 00:27:56

I would like to automate Internet Explorer 8 (using python 2.7 on Windows 7) machine. Here is my code after a post found on SO:

我想自动化Internet Explorer 8(在Windows 7上使用python 2.7)机器。以下是我在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)

I got the following error message for http://www.sap.com/austria/index.epx:

我得到了http://www.sap.com/ausa/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)

The code works perfectly for, e.g., google.com or bbc.com. Does anybody know what could be a reason?

这些代码非常适合google。com或bbc.com。有人知道是什么原因吗?

4 个解决方案

#1


11  

On IE9, you need to lower security settings to make the script work:

在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

#2


2  

Wow. I have been fighting with a script that was working for 3 days trying to figure out why it did not even reach the 10th line. Microsoft has been auto-updating Internet Explorer silently to IE10 throughout our organisation and it has caused major headaches for the CRM developers. I noticed now that the settings has been reset to default and that protected mode has been turned on.

哇。我一直在和一个工作了3天的脚本斗争,试图弄清楚为什么它甚至没有到达第十行。微软一直在自动更新ie浏览器到IE10在我们的组织和它已经引起了主要头痛的CRM开发人员。我注意到设置已经重置为默认设置,并且保护模式已经打开。

One of the most useful things you can try while developing your site is pushing F12 and setting the IE version to other versions. For example, your site used to work in IE9 but broke in 10. This allows you to be running IE10 and test your code in multiple versions. I am still trying to find a way to force certain websites to open in specific versions of internet explorer without having to push F12 every time.

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

#3


2  

I could not change my ie security setting but i found another solution that works in vbscript (dont ask me why i am using that :D)!

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

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

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 

#4


0  

I had somewhat similar issue, what I did was (however it was using mshtml and SHDocVw in C#.NET):

我遇到了一些类似的问题,我所做的是(然而它在c# .NET中使用mshtml和shvw):

  • Lowered the security (in security tab of Internet options) levels in internet explorer (just as you @Skarab had tried doing) ,
  • 降低Internet explorer的安全性(在Internet选项的security选项卡中)级别(就像您@Skarab尝试的那样),
  • Initialized internet explorer variable to a null value like:

    初始化internetexplorer变量为空值,如:

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

hope this helps...

希望这有助于……

#1


11  

On IE9, you need to lower security settings to make the script work:

在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

#2


2  

Wow. I have been fighting with a script that was working for 3 days trying to figure out why it did not even reach the 10th line. Microsoft has been auto-updating Internet Explorer silently to IE10 throughout our organisation and it has caused major headaches for the CRM developers. I noticed now that the settings has been reset to default and that protected mode has been turned on.

哇。我一直在和一个工作了3天的脚本斗争,试图弄清楚为什么它甚至没有到达第十行。微软一直在自动更新ie浏览器到IE10在我们的组织和它已经引起了主要头痛的CRM开发人员。我注意到设置已经重置为默认设置,并且保护模式已经打开。

One of the most useful things you can try while developing your site is pushing F12 and setting the IE version to other versions. For example, your site used to work in IE9 but broke in 10. This allows you to be running IE10 and test your code in multiple versions. I am still trying to find a way to force certain websites to open in specific versions of internet explorer without having to push F12 every time.

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

#3


2  

I could not change my ie security setting but i found another solution that works in vbscript (dont ask me why i am using that :D)!

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

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

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 

#4


0  

I had somewhat similar issue, what I did was (however it was using mshtml and SHDocVw in C#.NET):

我遇到了一些类似的问题,我所做的是(然而它在c# .NET中使用mshtml和shvw):

  • Lowered the security (in security tab of Internet options) levels in internet explorer (just as you @Skarab had tried doing) ,
  • 降低Internet explorer的安全性(在Internet选项的security选项卡中)级别(就像您@Skarab尝试的那样),
  • Initialized internet explorer variable to a null value like:

    初始化internetexplorer变量为空值,如:

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

hope this helps...

希望这有助于……