python - 如何修复 "Unknown error: Chrome failed to start: exited abnormally"以在 docker 中运行 chrome?

标签 python google-chrome selenium docker

我想使用 docker运行 selenium jenkins 内的测试实例。为了测试这一点,我正在创建一个带有以下 Dockerfile 的 docker 镜像。 :

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()

在我根据 here 的建议创建 selenium webdriver 之前和 herehere .但是,在创建 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)

如何解决?如何确保我可以使用 chrome (和 firefox )用于 docker 容器中的 Selenium 测试?

版本:
  • docker
  • Selenium 3.7.0
  • Nose -1.3.7
  • pyvirtualdisplay-0.2.1
  • xvfb 2:1.16.4-1+deb8u2
  • chromedriver=2.33.506092
  • Linux 4.10.0-37-通用 x86_64
  • 谷歌浏览器:62.0.3202.89-1
  • 最佳答案

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

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

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

    关于python - 如何修复 "Unknown error: Chrome failed to start: exited abnormally"以在 docker 中运行 chrome?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47203573/

    相关文章:

    python - 无法从 beautifulsoup 正确打印组合表

    python迷宫递归不递归

    python - 使用python从音频中获取时间戳

    html - 图片未出现在 Firefox/Chrome 中;在 IE 中正常

    java - TestNG 显示 0 测试运行

    java - 在 Selenium Java 中打开 href 链接

    Python Spacy 初学者 : similarities function

    python - Pandas 中矩阵最有效的行乘法

    javascript - 如何将变量传递到全局范围

    javascript - 我可以在新标签页上运行用户脚本吗?