Selenium python library via docker, Chrome error failed to start: exited abnormally

天大地大妈咪最大 提交于 2020-08-10 22:54:37

问题


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.

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)

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.

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 

回答1:


These options helped solving the issue.

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

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

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.

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.



来源:https://stackoverflow.com/questions/49710664/selenium-python-library-via-docker-chrome-error-failed-to-start-exited-abnorma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!