如何修复“未知错误:Chrome无法启动:异常退出”在docker中运行chrome?

时间:2017-11-09 13:50:17

标签: python google-chrome selenium docker

我想使用dockerselenium个实例中运行jenkins个测试。为了测试这一点,我正在使用以下Dockerfile

创建一个泊坞窗图像
FROM python:2.7-slim

WORKDIR /selenium

ADD . /selenium

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Install some basic stuff
RUN apt-get update -qqy
RUN apt-get install -y wget xvfb bzip2 zip unzip

# Install all the chrome libraries...
RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils

# Set firefox version and installation directory through environment variables.
ENV FIREFOX_VERSION 45.0
ENV FIREFOX_DIR /usr/bin/firefox
ENV FIREFOX_FILENAME $FIREFOX_DIR/firefox.tar.bz2

# Download the firefox of specified version from Mozilla and untar it.
RUN mkdir -p $FIREFOX_DIR
RUN wget -q --continue --output-document $FIREFOX_FILENAME "https://ftp.mozilla.org/pub/firefox/releases/${FIREFOX_VERSION}/linux-x86_64/en-US/firefox-${FIREFOX_VERSION}.tar.bz2"
RUN tar -xaf "$FIREFOX_FILENAME" --strip-components=1 --directory "$FIREFOX_DIR"
# Prepend firefox dir to PATH
ENV PATH $FIREFOX_DIR:$PATH


# Install Chrome
ENV LOCALEXE /usr/local/bin
ENV CHROMEDRIVERZIP=chromedriver_linux64.zip
ENV CHROMEDRIVER=chromedriver
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install
RUN wget "http://chromedriver.storage.googleapis.com/2.33/${CHROMEDRIVERZIP}"
RUN unzip ${CHROMEDRIVERZIP}
RUN mv ${CHROMEDRIVER} ${LOCALEXE}

# Prepare the output directories
RUN mkdir -p /selenium/out
RUN ln -sf /dev/stdout /selenium/out/out.log 
RUN ln -sf /dev/stderr /selenium/out/err.log

RUN Xvfb :99 -ac -screen 0 1280x1024x16 &
ENV DISPLAY :99


# Run app.py when the container launches
CMD ["nosetests", "-s", "test1.py"]

并在实际测试文件(test1.py)中我也有行

from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 800))  
display.start()

在我根据herehere以及here的建议创建selenium webdriver之前。但是,在创建了docker镜像并使用以下命令运行它之后

docker run --rm  selenium1
docker run --rm -e DISPLAY=$DISPLAY  selenium1
docker run --rm -e DISPLAY=$DISPLAY  -v /tmp/.X11-unix:/tmp/.X11-unix selenium1

我总是收到以下错误:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/selenium/test1.py", line 43, in test_fac
    driver = webdriver.Chrome(chrome_options=chrome_options)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 151, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 240, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
  WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.33.506092   (733a02544d189eeb751fe0d7ddca79a0ee28cce4),platform=Linux 4.10.0-37-generic x86_64)

如何解决?如何确保我可以在docker容器中使用chrome(和firefox)进行selenium测试?

版本:

  • docker
  • 硒-3.7.0
  • 鼻1.3.7
  • pyvirtualdisplay-0.2.1
  • xvfb 2:1.16.4-1 + deb8u2
  • chromedriver = 2.33.506092
  • Linux 4.10.0-37-generic x86_64
  • google-chrome:62.0.3202.89-1

1 个答案:

答案 0 :(得分:0)

您正在使用chromedriver=2.9.248304 selenium-3.7.0google-chrome: 62.0.3202.89-1

根据release notes,您应该使用此浏览器:

----------ChromeDriver v2.33 (2017-10-03)----------
Supports Chrome v60-62