首页 > 解决方案 > 无头 GCP Ubuntu 上的 Chromedriver 因“无法连接到渲染器”而崩溃

问题描述

$ cat /etc/os-release | grep VERSION=
VERSION="20.04.1 LTS (Focal Fossa)"
$ chromedriver --version
ChromeDriver 86.0.4240.22 (398b0743353ff36fb1b82468f63a3a93b4e2e89e-refs/branch-heads/4240@{#378})
$ chromium --version
Chromium 86.0.4240.183 snap

这是我用来运行的代码

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.headless = True
chrome_options.add_argument("--remote-debugging-port=9222")
driver = webdriver.Chrome(options=chrome_options)
driver.get("http://www.google.com")
print(driver.title)
print("works")

这是我经常收到的错误信息

  File "minimal_scraper_test.py", line 17, in <module>
    driver = webdriver.Chrome(options=chrome_options)
  File "/home/ubuntu/venv/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
    RemoteWebDriver.__init__(
  File "/home/ubuntu/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/ubuntu/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/ubuntu/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/ubuntu/venv/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
  (Session info: headless chrome=86.0.4240.183)

我查看了带有非常相似错误消息的问题。其中大多数是由于 chromedriver 和 chromium 之间的版本不匹配造成的。但是,我的两个版本都匹配。非常感谢你的帮助。

标签: pythonseleniumgoogle-chromeselenium-webdriverselenium-chromedriver

解决方案


事实证明,问题在于我有僵尸铬和 chromedriver 进程。杀死他们并重新运行我发布的代码片段。


推荐阅读