7

This is a pretty bizarre situation I have encountered. I have the following simple Python script:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options


options = Options()
options.add_argument("-headless")
browser = webdriver.Firefox(firefox_options=options)
browser.get("https://www.google.com")
print(browser.current_url)

And wrapper for the script:

#!/bin/bash

wget https://github.com/mozilla/geckodriver/releases/download/v0.19.1/geckodriver-v0.19.1-linux64.tar.gz
tar -xzvf geckodriver-v0.19.1-linux64.tar.gz
chmod 777 geckodriver
mv geckodriver /usr/bin/
firefox -v
# python3 when ubuntu
python test.py 

Additionally I have two Dockerfiles:

Dockerfile A (Ubuntu; works fine):

FROM ubuntu:16.04
RUN apt-get update -y && apt-get install -y python3 \
        python3-pip \
        firefox \
        build-essential \
        wget
COPY . /app
WORKDIR /app
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt
ENTRYPOINT ["bash"]
CMD ["test_wrapper.sh"]

Dockerfile B (Debian; crashes):

FROM continuumio/anaconda3:5.0.1
RUN apt-get update -y && apt-get install -y iceweasel \
        build-essential \
        wget
COPY . /app
WORKDIR /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ENTRYPOINT ["bash"]
CMD ["test_wrapper.sh"]

test.py run from the image built from Dockerfile B throws the following exception:

selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status: 1

The geckodriver.log shows the following error:

GTK_BACKEND doesn't match available displays

Has anyone encountered this and know a workaround? It shouldn't need access to a display because it's running headless - unless selenium Firefox options are different in iceweasel than regular Firefox? I expected similar behavior, and I would much prefer to use the Anaconda image

I just tried this which I was almost certain would solve it but it did not work.

EDIT: I do not believe this is a geckodriver issue as I tried the same Dockerfile with firefox-esr instead of iceweasel. Additionally, I tried starting the container interactively and executed firefox -headless (which on ubuntu launches a headless firefox session) it gave the same exact GTK error selenium does.

4

2 回答 2

3
RUN apt-get install -y --no-install-recommends apt-utils
RUN apt-get install -y wget \
        build-essential \
        libgl1-mesa-glx \
        libgtk-3-dev 
ARG FIREFOX_VERSION=58.0.2
RUN wget --no-verbose -O /tmp/firefox.tar.bz2 https://download-installer.cdn.mozilla.net/pub/firefox/releases/$FIREFOX_VERSION/linux-x86_64/en-US/firefox-$FIREFOX_VERSION.tar.bz2 \
   && rm -rf /opt/firefox \
   && tar -C /opt -xjf /tmp/firefox.tar.bz2 \
   && rm /tmp/firefox.tar.bz2 \
   && mv /opt/firefox /opt/firefox-$FIREFOX_VERSION \
   && ln -fs /opt/firefox-$FIREFOX_VERSION/firefox /usr/bin/firefox
ARG GK_VERSION=v0.19.1
RUN wget --no-verbose -O /tmp/geckodriver.tar.gz http://github.com/mozilla/geckodriver/releases/download/$GK_VERSION/geckodriver-$GK_VERSION-linux64.tar.gz \
   && rm -rf /opt/geckodriver \
   && tar -C /opt -zxf /tmp/geckodriver.tar.gz \
   && rm /tmp/geckodriver.tar.gz \
   && mv /opt/geckodriver /opt/geckodriver-$GK_VERSION \
   && chmod 755 /opt/geckodriver-$GK_VERSION \
   && ln -fs /opt/geckodriver-$GK_VERSION /usr/bin/geckodriver

根据@Florent B. 链接的内容进行以下更改是它的修复。本质上firefox-esr是 52 版-headless,Firefox 的选项是在 55 版中发布的。我不确定 iceweasel 是什么版本,但大概比这更早。此外,52 以上的 Firefox 版本在没有libgtk-3. 我认为 Debian 8 仍然有libgtk-2,所以也需要安装它。无头浏览在 Debian 8 上与build-essentiallibgtk-3-devwget和完美配合。libgl1-mesa-glx

于 2018-03-10T19:18:29.363 回答
2

你永远不应该使用包管理器安装 Firefox。您可以在下面的链接中找到所有的 Firefox 版本

https://download-installer.cdn.mozilla.net/pub/firefox/releases/

如果您查看 Selenium Firefox Dockerfile

https://github.com/SeleniumHQ/docker-selenium/blob/master/NodeFirefox/Dockerfile

他们使用下载所需的版本

wget --no-verbose -O /tmp/firefox.tar.bz2 $FIREFOX_DOWNLOAD_URL

对于依赖问题,他们运行以下命令

apt-get -qqy --no-install-recommends install firefox

然后稍后删除firefox

apt-get -y purge firefox

这确保您无需担心所有必需的依赖项。如果您愿意,您仍然可以获得依赖项

$ apt-cache depends firefox | grep Depends
  Depends: lsb-release
  Depends: libatk1.0-0
  Depends: libc6
  Depends: libcairo-gobject2
  Depends: libcairo2
  Depends: libdbus-1-3
  Depends: libdbus-glib-1-2
  Depends: libfontconfig1
  Depends: libfreetype6
  Depends: libgcc1
  Depends: libgdk-pixbuf2.0-0
  Depends: libglib2.0-0
  Depends: libgtk-3-0
  Depends: libpango-1.0-0
  Depends: libpangocairo-1.0-0
  Depends: libstartup-notification0
  Depends: libstdc++6
  Depends: libx11-6
  Depends: libx11-xcb1
  Depends: libxcb-shm0
  Depends: libxcb1
  Depends: libxcomposite1
  Depends: libxdamage1
  Depends: libxext6
  Depends: libxfixes3
  Depends: libxrender1
  Depends: libxt6

如果你看下面的链接

https://developer.mozilla.org/en-US/Firefox/Headless_mode

浏览器支持

Headless Firefox 适用于 Linux 上的 Fx55+,以及 Windows/Mac 上的 56+。

于 2018-03-10T19:12:37.897 回答