【问题标题】:Selenium python library via docker, Chrome error failed to start: exited abnormallySelenium python库通过docker,Chrome错误无法启动:异常退出
【发布时间】:2018-09-17 12:53:14
【问题描述】:

我正在尝试从基于 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 build 是如何工作的,我猜 docker 映像是作为 @ 构建的987654326@,里面的所有代码都以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 

【问题讨论】:

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


    【解决方案1】:

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

    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,如果你尝试一下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 2016-10-08
      • 2020-11-01
      相关资源
      最近更新 更多