python - 无头 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 之间的版本不匹配造成的。但是,我的两个版本都匹配。非常感谢你的帮助。
解决方案
事实证明,问题在于我有僵尸铬和 chromedriver 进程。杀死他们并重新运行我发布的代码片段。
推荐阅读
- python - 在 Python 中计算数据框中的单词
- c - execve(2) 系统命令在执行前退出
- javascript - Fabricjs .toJSON() 无法正确保存到 LocalStorage ([object Object])
- javascript - 从 $arrayElemAt 结果中选择字段
- javascript - 在 JavaScript 中使用反引号(反引号)将导致“无法读取属性 'style' of null '
- tfs - 同一个 TFS 版本的两个实例能否共享一个数据层
- php - 从php中的字符串组中获取替代字符串
- r - 我可以使用 R 中的循环自动执行此代码吗?
- python - 正则表达式(re.search)无法检测错误
- c# - foreachasync 和 for 循环与实体记录的区别?