【问题标题】:How to fix "Unknown error: Chrome failed to start: exited abnormally" for running chrome in docker?如何修复“未知错误:Chrome 无法启动:异常退出”以在 docker 中运行 chrome?
【发布时间】:2018-04-22 13:10:30
【问题描述】:

我想使用dockerjenkins 实例中运行selenium 测试。为了测试这一点,我正在使用以下 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()

根据hereherehere 的建议,在我创建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)进行硒测试?

版本:

  • 码头工人
  • 硒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-generic x86_64
  • 谷歌浏览器:62.0.3202.89-1

【问题讨论】:

    标签: python google-chrome selenium docker


    【解决方案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
    

    【讨论】:

    • 我更改了驱动,但出现类似错误: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)
    猜你喜欢
    • 2014-04-20
    • 2016-10-08
    • 2023-01-07
    • 2018-07-17
    • 2017-09-07
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多