WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally with ChromeDriver Chrome and Selenium through Python on VPS

WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally with ChromeDriver Chrome and Selenium through Python on VPS

所以我遇到了与这些帖子完全相同的错误

Selenium 'Chrome failed to start: exited abnormally' error

Unknown error: Chrome failed to start: exited abnormally

我尝试了他们推荐的方法,但没有用。

这是我的代码

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-extensions')
options.add_argument('--headless')
options.add_argument('--disable-gpu')

driver = webdriver.Chrome(chrome_options=options)
driver.get('http://nytimes.com')
print(driver.title)

driver.close()

这里是完整的错误信息

Traceback (most recent call last):
  File "seleniumtest.py", line 13, in <module>
    driver = webdriver.Chrome(chrome_options=options)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.15.0-42-generic x86_64)

我到底做错了什么?我是 运行 在 digitalocean 上的 ubuntu VPS。

您尚未提供浏览器的版本,但 chromedriver 2.30 相当 旧 - 大约 2017 年 6 月; Chrome 当时是 59 版,现在是 72 版。 (yes, I checked, it's not like I know their release history by heart )

我建议将其升级到最新版本 - 或者升级到与您安装的浏览器相匹配的版本。

这个错误信息...

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.15.0-42-generic x86_64)

...表示 ChromeDriver 无法 initiate/spawn 新的 WebBrowserChrome 浏览器 session.

正好有两个 不兼容 问题,如下所述。


disable-gpu

Headless Chrome 首次由 [=170 作为 GA(通用版) 发布时=] 团队 文章Getting Started with Headless Chrome 提到:

--disable-gpu \                # Temporarily needed if running on Windows.

注释已添加为:

Right now, you'll also want to include the --disable-gpu flag if you're running on Windows.

根据讨论 Headless: make --disable-gpu flag unnecessary 很明显:

The --disable-gpu flag is no longer necessary on Linux or Mac OSX. It will also become unnecessary on Windows as soon as the bug SwiftShader fails an assert on Windows in headless mode is fixed. Now as this issue is marked fixed the argument --disable-gpu should be redundant now.

注意:您可以在ERROR:gpu_process_transport_factory.cc(1007)-Lost UI shared context : while initializing Chrome browser through ChromeDriver in Headless mode

中找到详细的讨论

但是,您的主要问题是您使用的二进制文件版本之间 不兼容,如下所示:

  • 您正在使用 chromedriver=2.30
  • chromedriver=2.30 的发行说明清楚地提到了以下内容:

Supports Chrome v58-60

  • 我们不知道您的 chrome 版本。假设您正在使用最新的 Chrome 版本之一:
    • Chrome version 71
    • Chrome version 72
    • Chrome version 73

所以 ChromeDriver v2.30Chrome 浏览器 v71- 73

解决方案

  • 根据你的Chrome浏览器版本升级ChromeDriver相应地遵循guidelines 下面:
    • 如果您使用的是Chrome版本73,您需要下载ChromeDriver73.0.3683.20
    • 如果您使用的是Chrome版本72,您需要下载ChromeDriver2.46 或 ChromeDriver 72.0.3626.69
    • 如果您使用的是Chrome版本71,您需要下载ChromeDriver2.46 或 ChromeDriver 71.0.3578.137
    • Chrome的旧版本,ChromeDriver的版本见this discussion 支持。

参考资料

您可以在以下位置找到一些相关讨论: