vba Internet Explorer 自动化不适用于 IE11

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25488988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:22:30  来源:igfitidea点击:

Internet Explorer automation not working with IE11

vbainternet-explorer

提问by python dude

I have some code that retrieves data from multiple websites via Internet Explorer automation in VBA. My code worked without problems with IE8, but in IE11, after the Navigate method of the Internet Explorer object is called, the Document and LocationURL are not updated; they still refer to the previously displayed website. Here's some code to reproduce the problem:

我有一些代码可以通过 VBA 中的 Internet Explorer 自动化从多个网站检索数据。我的代码在 IE8 下运行没有问题,但是在 IE11 中,调用 Internet Explorer 对象的 Navigate 方法后,Document 和 LocationURL 没有更新;他们仍然指的是以前显示的网站。这是一些重现问题的代码:

Sub Test()
    Debug.Print "start"
    Dim ie
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.Navigate "http://en.wikipedia.org/wiki/Visual_Basic"
    wait ie
    Debug.Print "Current URL: " & ie.LocationURL
    ie.Navigate "http://en.wikipedia.org/wiki/Microsoft_Office"
    wait ie
    Debug.Print "Current URL: " & ie.LocationURL
    Set ie = Nothing
End Sub

Sub Wait(ie As Variant)
    Do While ie.Busy
        Application.wait DateAdd("s", 1, Now)
    Loop
End Sub

When a run the above Test sub on a machine with IE8, it prints two different URLs, which is the expected behavior. However, when I run the same code on a machine with IE11, it prints the first URL twice. Any idea what might be wrong?

当在带有 IE8 的机器上运行上述 Test sub 时,它会打印两个不同的 URL,这是预期的行为。但是,当我在装有 IE11 的机器上运行相同的代码时,它会打印第一个 URL 两次。知道可能有什么问题吗?

Update: I couldn't find a solution, so I went for the workaround of opening a new IE window for each URL.

更新:我找不到解决方案,所以我找到了为每个 URL 打开一个新 IE 窗口的解决方法。

回答by Eric Tedj - MSFT

I am not familiar with the VBA IE automation that you are using, however it sounds like you are hitting the same issue as Selenium WebDriver on IE11.

我不熟悉您使用的 VBA IE 自动化,但是听起来您遇到了与 IE11 上的 Selenium WebDriver 相同的问题。

You may need to follow the same steps provided by the in the Selenium Wiki.

您可能需要遵循 Selenium Wiki 中提供的相同步骤。

For IE 11 only, you will need to set a registry entry on the target computer so that the driver can maintain a connection to the instance of Internet Explorer it creates. For 32-bit Windows installations, the key you must examine in the registry editor is HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE. For 64-bit Windows installations, the key is HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE. Please note that the FEATURE_BFCACHE subkey may or may not be present, and should be created if it is not present. Important: Inside this key, create a DWORD value named iexplore.exe with the value of 0.

仅对于 IE 11,您需要在目标计算机上设置一个注册表项,以便驱动程序可以保持与它创建的 Internet Explorer 实例的连接。对于 32 位 Windows 安装,您必须在注册表编辑器中检查的键是 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE。对于 64 位 Windows 安装,密钥是 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE。请注意 FEATURE_BFCACHE 子项可能存在也可能不存在,如果不存在则应创建。重要提示:在此键中,创建一个名为 iexplore.exe 且值为 0 的 DWORD 值。

http://code.google.com/p/selenium/wiki/InternetExplorerDriver

http://code.google.com/p/selenium/wiki/InternetExplorerDriver

Hopefully that fixes your problem!

希望能解决您的问题!

回答by Jony

THX, You helped me. W7 Ultimate 64bit czech, IE11, VBA in Microstation V8i I use code like:

THX,你帮了我。W7 Ultimate 64bit czech、IE11、Microstation V8i 中的 VBA 我使用如下代码:

Public explorer As Object
....
Set explorer = CreateObject("InternetExplorer.Application")
....

If InStr(explorer.LocationURL, "CAPTCHA") = 0 Then
...
End If