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

发布于 2024-11-19 08:31:54 字数 2017 浏览 3 评论 0原文

我想自动化 Internet Explorer 8(在 Windows 7 上使用 python 2.7)机器。这是我在 找到的帖子之后的代码关于

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 等网站。有谁知道可能是什么原因?

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:

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

待天淡蓝洁白时 2024-11-26 08:31:54

在 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

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

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
勿忘初心 2024-11-26 08:31:54

我无法更改我的 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 

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)!

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 
嗫嚅 2024-11-26 08:31:54

哇。我一直在与一个运行了 3 天的脚本进行斗争,试图找出为什么它甚至没有到达第 10 行。 Microsoft 一直在整个组织内悄悄地将 Internet Explorer 自动更新到 IE10,这给 CRM 开发人员带来了很大的麻烦。我现在注意到设置已重置为默认值并且保护模式已打开。

在开发网站时,您可以尝试的最有用的事情之一是按 F12 并将 IE 版本设置为其他版本。例如,您的网站以前可以在 IE9 中运行,但在 10 中就崩溃了。这允许您运行 IE10 并在多个版本中测试您的代码。我仍在尝试找到一种方法来强制某些网站在特定版本的 Internet Explorer 中打开,而不必每次都按 F12。

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.

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.

深海不蓝 2024-11-26 08:31:54

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

  • 降低了 Internet Explorer 中的安全性(在 Internet 选项的安全选项卡中)级别(就像您 @Skarab 尝试过的那样),
  • 将 Internet Explorer 变量初始化为空值,例如:

    /*将浏览器变量初始化为空值*/
    SHDocVw.InternetExplorer 即 =null;
    即 = new SHDocVw.InternetExplorer();
    

希望这有帮助...

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

  • Lowered the security (in security tab of Internet options) levels in internet explorer (just as you @Skarab had tried doing) ,
  • Initialized internet explorer variable to a null value like:

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

hope this helps...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文