selenium.common.exceptions.WebDriverException:消息:未知错误:无法通过 Selenium 使用 ChromeDriver 发现打开的页面

k.l*_*.lo 5 selenium webdriver python-3.x selenium-chromedriver selenium-webdriver

在此处输入图片说明我是硒的新手,只是想学习。尝试通过 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)
Run Code Online (Sandbox Code Playgroud)

尝试了很多谷歌搜索,但没有任何帮助。下面是我的代码:

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)
Run Code Online (Sandbox Code Playgroud)

这不是要处理的代码,但我很困惑我在这里缺少什么。还请推荐一些好的在线资源来学习 Python Selenium。我打开的 chrome 如下所示:

Deb*_*anB 8

这个错误信息...

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)
Run Code Online (Sandbox Code Playgroud)

...暗示ChromeDriver无法启动/生成新的WebBrowser,Chrome 浏览器会话。


解决方案

添加参数--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()
Run Code Online (Sandbox Code Playgroud)

其他注意事项

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