问题
from selenium import webdriver
import unittest
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
class MyTestCase(unittest.TestCase):
def setUp(self) -> None:
self.driver = webdriver.Ie(executable_path="C:\\webdriver\\IEDriverServer.exe")
self.driver.maximize_window()
self.driver.get("https://www.google.ca")
def test_googletest(self):
element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.NAME, "q")))
element.send_keys("System")
sleep(3)
def tearDown(self):
self.driver.close()
if __name__ == '__main__':
unittest.main()
The code is opening the IE browser successfully but unable to get to google's website. It gets stuck on the localhost in the address bar and after some time it times out. See image below
Below is the error that I get:
Error
Traceback (most recent call last):
File "C:\Users\mahalr\AppData\Local\Programs\Python\Python38-32\lib\unittest\case.py", line 60, in testPartExecutor
yield
File "C:\Users\mahalr\AppData\Local\Programs\Python\Python38-32\lib\unittest\case.py", line 672, in run
self._callSetUp()
File "C:\Users\mahalr\AppData\Local\Programs\Python\Python38-32\lib\unittest\case.py", line 630, in _callSetUp
self.setUp()
File "C:\Users\mahalr\PycharmProjects\TestAutomation\TestAutomation.py", line 13, in setUp
self.driver.get("https://www.google.ca")
File "C:\Users\mahalr\PycharmProjects\TestAutomation\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 333, in get
self.execute(Command.GET, {'url': url})
File "C:\Users\mahalr\PycharmProjects\TestAutomation\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\mahalr\PycharmProjects\TestAutomation\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Failed to navigate to https://www.google.ca. This usually means that a call to the COM method IWebBrowser2::Navigate2() failed. The error returned is: Received error: 0x80004005 ['Unspecified error']
Here is my pre-test setup:
- Windows 10 64 bit laptop
- IE Server.exe 32 bit
- IE11
- DWORD key created under Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Internet Explorer\Main\FeatureControl with a name FEATURE_BFCACHE value = 0
- Enable protected mode checkbox checked for all zones under Internet Options -> Security tab
- Enable 64-bit processes for Enhanced Protected Mode (checked) - This option is disabled and checked as since it is my company laptop
Can someone please guide me as I have hit a wall with this issue and unable to find a resolution?
回答1:
This error message...
selenium.common.exceptions.WebDriverException: Message: Failed to navigate to https://www.google.ca. This usually means that a call to the COM method IWebBrowser2::Navigate2() failed. The error returned is: Received error: 0x80004005 ['Unspecified error']
...implies that the framework for driving internet-explorer-11 couldn't instantiate the browser as a COM object.
History of Protected Mode settings for Internet Explorer
@JimEvans in the article You're Doing It Wrong: IE Protected Mode and WebDriver clearly mentioned, while automating internet-explorer through iedriverserver:
A browser session was represented by a single instance of the iexplore.exe executable. A framework for driving IE could instantiate the browser as a COM object using CoCreateInstance(), or could easily get the COM interfaces to a running instance by using the presence of ActiveAccessibility and sending a
WM_HTML_GETOBJECT
message to the appropriate IE window handle. Once the framework had a pointer to the COM interfaces, you could be sure that they'd be valid for the lifetime of the browser. It also meant you could easily attach to the events fired by the browser through the DWebBrowserEvents2 COM interface.
Then along came the combination of IE 7 and Windows Vista. In and effort to reduce the attack surface presented by malicious web sites, IE 7 introduced something called Protected Mode, which leveraged Mandatory Integrity Control in Windows Vista to prevent actions initiated IE, usually initiated by JavaScript, from being able to access the operating system the way it could in prior releases. While this was generally a welcome development for most users of IE, it created all manner of problems for automating IE.
When you cross into or out of Protected Mode by, say, navigating from an internal intranet website to one on the internet, IE has to create a new process, because it can't change the Mandatory Integrity Control level of the existing process. Moreover, in IE versions after 7, it's not always obvious that a Protected Mode boundary has been crossed, since IE tries to present a better user experience by seamlessly merging the browser window of the new process with the already opened browser window. This under-the-covers process switching also means that any references pointing to IE's COM objects before the Protected Mode boundary crossing are left pointing to objects that are no longer used by IE after the boundary crossing.
Moreover, If you look into the Required Configuration of Internet Explorer Driver it is clearly mentioned:
Protected Mode settings
On Internet Explorer 7 or higher on Windows Vista or Windows 7, you must set the Protected Mode settings for each zone to be the same value. The value can be on
or off
, as long as it is the same for every zone. To set the Protected Mode settings you have to choose Internet Options
from the Tools
menu and then click on the Security tab
. For each zone, there will be a check box at the bottom of the tab labeled Enable Protected Mode
.
Solution
You need to ensure that the Protected Mode settings for each zone to be the same value.
References
You can find a couple of relevant discussions in:
- Internet Explorer Protective mode setting and Zoom levels
来源:https://stackoverflow.com/questions/62901867/failed-to-navigate-to-https-www-google-ca-this-usually-means-that-a-call-to-t