通过docker使用Selenium python库,Chrome报错无法启动:异常退出

TPP*_*PPZ 4 google-chrome xvfb selenium-chromedriver selenium-webdriver docker

我试图selenium在基于 miniconda/anaconda 的 docker 容器内使用该库运行一些 python 脚本,但我不断收到此错误:selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally。我还使用 python 包装器来xvfb避免打开真正的 Chrome 窗口。

要重现此内容(从正在运行的 docker 容器):

root@304ccd3bae83:/opt# python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> 
>>> from selenium import webdriver
>>> from xvfbwrapper import Xvfb
>>> 
>>> with Xvfb(width=1366, height=768) as xvfb:
...     my_driver = webdriver.Chrome('/opt/chromedriver/2.33/chromedriver')
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/opt/conda/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
    desired_capabilities=desired_capabilities)
  File "/opt/conda/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 151, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/opt/conda/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 240, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/opt/conda/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
    self.error_handler.check_response(response)
  File "/opt/conda/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, 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.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4),platform=Linux 4.4.0-116-generic x86_64)
Run Code Online (Sandbox Code Playgroud)

据此:https: //sites.google.com/a/chromium.org/chromedriver/help/chrome-doesn-t-start似乎有人可能需要使用适用于所有用户的独立版本的 Chrome ,但我不确定 docker 构建是如何工作的,我猜 docker 镜像是构建为root,并且其中的所有代码都是执行为root,所以不同用户控制 Chrome 应该不会有任何问题。

这段 python 代码在带有 X windows 的普通 Ubuntu 笔记本电脑上运行良好。当从正在运行的 docker 容器中检查时,我需要仔细选择 Chrome 和 chromedriver 的版本:

root@304ccd3bae83:/opt# /opt/chromedriver/2.33/chromedriver --version
ChromeDriver 2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4)
root@304ccd3bae83:/opt# google-chrome-stable --version
Google Chrome 62.0.3202.75 
Run Code Online (Sandbox Code Playgroud)

TPP*_*PPZ 7

这些选项有助于解决问题。

chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("--disable-setuid-sandbox")
Run Code Online (Sandbox Code Playgroud)

看到时需要其中之一Chrome failed to start: crashed

chrome-driver另外:确保用于ps aux | grep chrome-driver查找要杀死的 PID 的进程没有僵尸(来自之前的执行) 。

请记住,如果您使用 Pythonmultiprocessing库生成许多涉及其自己的 Chrome 浏览器实例的进程,那么您不能使用 Docker(它应该只启动一个 Python 进程,除非使用类似的东西supervisor),所以您可以看看:selenium.common.exceptions.WebDriverException: Message: chrome not reachable如果你无论如何都尝试一下。