Selenium常见异常:WebDriverException:Message:未知错误:无法通过Selenium使用ChromeDriver发现打开的页面

5

在此输入图像描述我是一个刚开始学习selenium的新手。当我尝试通过ChromeDriver打开Chrome浏览器时,出现了以下错误:

Traceback (most recent call last):
  File "selenium_practise1_chrome.py", line 5, in <module>
    driver = webdriver.Chrome()
  File "C:\Users\kulokesh\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 75, in __init__
    desired_capabilities=desired_capabilities)
  File "C:\Users\kulokesh\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 156, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\kulokesh\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 251, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\kulokesh\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "C:\Users\kulokesh\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: unable to discover open pages
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=a lotows NT 6.1.7601 SP1 x86_64)

我尝试了很多谷歌搜索,但没有什么帮助。以下是我的代码:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get('http://www.python.org')
print(driver.title)

这不是一个需要处理的代码,但我很困惑,我错过了什么。 同时,请推荐一些学习Python Selenium的好的在线资源。 我的Chrome浏览器看起来像下面这样:


1个回答

10

这个错误信息...

selenium.common.exceptions.WebDriverException: Message: unknown error: unable to discover open pages
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=a lotows NT 6.1.7601 SP1 x86_64)

这意味着ChromeDriver无法启动/生成新的WebBrowserChrome浏览器会话。


解决方案

将参数--no-sandbox通过ChromeOptions()添加到您现有的代码中,如下所示:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--no-sandbox') # Bypass OS security model
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe')
driver.get('http://www.python.org')
print(driver.title)
driver.quit()

其他注意事项

  • 升级 Selenium 至当前版本 Version 3.14.0
  • ChromeDriver 升级至当前版本 ChromeDriver v2.41
  • 保持 Chrome 版本在 Chrome v66-68 之间。 (根据 ChromeDriver v2.41 发行说明)
  • 通过你的 IDE 清理你的 项目工作区,并仅使用所需的依赖项重新构建项目。
  • 执行你的 @Test
  • 始终在 tearDown(){} 方法中调用 driver.quit() 以优雅地关闭并销毁 WebDriverWeb Client 实例。

仍然没有帮助,仍然出现相同的问题:“selenium.common.exceptions.WebDriverException: Message: unknown error: unable to discover open window in chrome (Session info: chrome=67.0.3396.79) (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 6.1.7601 SP1 x86_64)” - k.lo
1
请查看我的更新答案并告诉我状态。 - undetected Selenium
1
非常好用,谢谢。你能推荐我学习Python中Selenium的最佳资源吗? - k.lo
1
@k.lo 官方Selenium文档和非官方WebDriver API是学习Python中Selenium最好的文档。 - undetected Selenium
1
这个答案没有解释 --no-sandbox 是什么,也没有说明为什么它可以解决问题。显然,这个答案建议禁用一个默认启用的安全功能。通常这是一个不好的解决方案的表现。这个答案需要改进。 - Claus
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接