无法导航到https://www.google.ca.这通常意味着使用Python Unittest Selenium调用COM方法IWebBrowser2 :: Navigate2() [英] Failed to navigate to https://www.google.ca. This usually means that a call to the COM method IWebBrowser2::Navigate2() with Python Unittest Selenium

查看:192
本文介绍了无法导航到https://www.google.ca.这通常意味着使用Python Unittest Selenium调用COM方法IWebBrowser2 :: Navigate2()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

硒导入Webdriver中的

 导入单元测试从selenium.webdriver.support.ui导入WebDriverWait从selenium.webdriver.support导入EC的预期条件来自selenium.webdriver.common.by导入方式从进口睡眠开始类MyTestCase(unittest.TestCase):def setUp(self)->没有任何:self.driver = webdriver.Ie(executable_path ="C:\\ webdriver \\ IEDriverServer.exe")self.driver.maximize_window()self.driver.get(" https://www.google.ca")def test_googletest():元素= WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.NAME,"q"))))element.send_keys(系统")睡眠(3)def tearDown(自己):self.driver.close()如果__name__ =='__main__':unittest.main() 

该代码已成功打开IE浏览器,但无法访问Google网站.它卡在地址栏中的本地主机上,一段时间后超时.见下图

以下是我得到的错误:

 错误追溯(最近一次通话):在testPartExecutor中的第60行中的文件"C:\ Users \ mahalr \ AppData \ Local \ Programs \ Python \ Python38-32 \ lib \ unittest \ case.py"屈服运行中的文件"C:\ Users \ mahalr \ AppData \ Local \ Programs \ Python \ Python38-32 \ lib \ unittest \ case.py",第672行self._callSetUp()_callSetUp中的文件"C:\ Users \ mahalr \ AppData \ Local \ Programs \ Python \ Python38-32 \ lib \ unittest \ case.py",第630行self.setUp()setUp中第13行的文件"C:\ Users \ mahalr \ PycharmProjects \ TestAutomation \ TestAutomation.py"self.driver.get(" https://www.google.ca")在获取的文件"C:\ Users \ mahalr \ PycharmProjects \ TestAutomation \ venv \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py"中,第333行self.execute(Command.GET,{'url':url})在执行的文件"C:\ Users \ mahalr \ PycharmProjects \ TestAutomation \ venv \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py"中执行self.error_handler.check_response(响应)文件"C:\ Users \ mahalr \ PycharmProjects \ TestAutomation \ venv \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py",第242行,在check_response中引发exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.WebDriverException:消息:无法导航到https://www.google.ca.这通常意味着对COM方法IWebBrowser2 :: Navigate2()的调用失败.返回的错误是:收到的错误:0x80004005 ['Unspecified error'] 

这是我的测试前设置:

  1. Windows 10 64位笔记本电脑
  2. IE Server.exe 32位
  3. IE11
  4. 在名称为FEATURE_BFCACHE的值= 0的Computer \ HKEY_LOCAL_MACHINE \ SOFTWARE \ WOW6432Node \ Microsoft \ Internet Explorer \ Main \ FeatureControl下创建的DWORD密钥
  5. 选中"Internet选项"下所有区域的启用保护模式"复选框->安全标签
  6. 为增强保护模式启用64位进程(已选中)-由于该选项是我公司的笔记本电脑,因此该选项已被禁用和选中

有人遇到我这个问题而无法找到解决方法时,可以帮助我吗?

解决方案

此错误消息...

  selenium.common.exceptions.WebDriverException:消息:无法导航至https://www.google.ca.这通常意味着对COM方法IWebBrowser2 :: Navigate2()的调用失败.返回的错误是:收到的错误:0x80004005 ['Unspecified error'] 

...表示驱动

 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:

  1. Windows 10 64 bit laptop
  2. IE Server.exe 32 bit
  3. IE11
  4. DWORD key created under Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Internet Explorer\Main\FeatureControl with a name FEATURE_BFCACHE value = 0
  5. Enable protected mode checkbox checked for all zones under Internet Options -> Security tab
  6. 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?

解决方案

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 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 through :

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:

这篇关于无法导航到https://www.google.ca.这通常意味着使用Python Unittest Selenium调用COM方法IWebBrowser2 :: Navigate2()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆