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

时间:2018-04-07 18:35:10

标签: google-chrome docker selenium-webdriver selenium-chromedriver xvfb

我正在尝试使用基于miniconda / anaconda的docker容器中的selenium库运行一些python脚本,但我不断收到此错误:selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally。我还使用xvfb的python包装器来避免打开真正的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)

根据这一点: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 

1 个答案:

答案 0 :(得分:2)

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

chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("--disable-setuid-sandbox")

在看到Chrome failed to start: crashed时需要其中一个。

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

请记住,如果您使用Python multiprocessing库来生成涉及他们自己的Chrome浏览器实例的许多进程,那么您就不能使用Docker(它应该只启动一个Python进程,除非使用supervisor)这样的内容,如果您尝试的话,可能会看到selenium.common.exceptions.WebDriverException: Message: chrome not reachable

相关问题