Selenium Firefox webdriver适用于从Ubuntu构建的图像,但不适用于使用Debian构建的图像

qua*_*tik 5 python debian selenium docker iceweasel

这是我遇到的一个非常奇怪的情况.我有以下简单的Python脚本:

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)
Run Code Online (Sandbox Code Playgroud)

和脚本的包装:

#!/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 
Run Code Online (Sandbox Code Playgroud)

另外我有两个Dockerfiles:

Dockerfile A(Ubuntu;工作正常):

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"]
Run Code Online (Sandbox Code Playgroud)

Dockerfile B(Debian;崩溃):

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"]
Run Code Online (Sandbox Code Playgroud)

test.py 从Dockerfile B构建的映像运行会引发以下异常:

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

geckodriver.log显示以下错误:

GTK_BACKEND doesn't match available displays

有没有人遇到这个并知道一个解决方法?它应该不需要访问显示器,因为它运行无头 - 除非selenium Firefox选项在iceweasel中与普通Firefox不同?我期待类似的行为,我更喜欢使用Anaconda图像

我刚试过这个,我几乎可以肯定会解决它,但它没有用.

编辑:我不相信这是一个geckodriver问题,因为我尝试使用相同的Dockerfile firefox-esr代替iceweasel.另外,我尝试以交互方式启动容器并执行firefox -headless(在ubuntu上启动无头的firefox会话)它给出了与selenium完全相同的GTK错误.

qua*_*tik 4

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
Run Code Online (Sandbox Code Playgroud)

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