通过docker的Selenium python库,Chrome错误无法启动:异常退出 [英] Selenium python library via docker, Chrome error failed to start: exited abnormally

查看:257
本文介绍了通过docker的Selenium python库,Chrome错误无法启动:异常退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在基于miniconda / anaconda的Docker容器中使用 selenium 库运行一些python脚本,但我不断收到此错误: selenium.common.exceptions.WebDriverException:消息:未知错误:Chrome无法启动:异常退出。我还对 xvfb 使用python包装程序以避免打开真正的Chrome窗口。

I am trying to run some python scripts with the selenium library from within a docker container based on miniconda/anaconda, but I keep getting this error: selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally. I am also using a python wrapper for xvfb to avoid opening a real Chrome window.

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

To reproduce this (from a running docker container):

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 构建的,并且其中的所有代码都是作为<$ c执行的$ c> root ,因此控制Chrome的不同用户应该不会有任何问题。

According to this: https://sites.google.com/a/chromium.org/chromedriver/help/chrome-doesn-t-start it seems someone may need to use a stand-alone version of Chrome that works for all users, but I am not sure how the docker build works, I guess the docker image is built as root, and all the code inside it is executed as root, so there should not be any issue with different users controlling Chrome.

此python代码在普通的Ubuntu笔记本电脑上可以正常工作X视窗。从运行的Docker容器中进行检查时,我需要仔细选择Chrome和chromedriver的版本:

This python code works fine on a normal Ubuntu laptop with X windows. I need to carefully pick both the versions of Chrome and chromedriver, at the moment when checking from within the running docker container:

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 


推荐答案

这些选项有助于解决

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

一个当看到 Chrome无法启动:崩溃时,需要其中的一个。

One of them is needed when seeing Chrome failed to start: crashed.

另外:确保没有僵尸( ($$$$$)的 chrome-driver 进程使用| grep chrome-driver 查找要杀死的PID。

Also: make sure there are no zombies (from previous executions) for the chrome-driver process using ps aux | grep chrome-driver to find the PIDs to kill.

请记住,如果您使用的是Python 多处理程序库生成许多涉及他们自己的Chrome浏览器实例的进程,那么您将无法使用Docker(应该仅启动一个Python进程,除非使用 supervisor ),因此您可能会看到: selenium.common.exceptions.WebDriverException:消息:如果无法尝试,则无法访问chrome

Bare in mind that if you are using the Python multiprocessing library to spawn many processes involving their own instance of the Chrome browser, then you can not use Docker (which is supposed to start just one Python process, unless using stuff like supervisor), so you may see: selenium.common.exceptions.WebDriverException: Message: chrome not reachable if you try anyway.

这篇关于通过docker的Selenium python库,Chrome错误无法启动:异常退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆