Selenium Firefox Webdriver在Ubuntu构建的图像上工作,但在Debian构建的图像上工作



这是我遇到的非常奇怪的情况。我有以下简单的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)

和脚本包装器:

#!/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 

此外,我还有两个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"]

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从dockerfile b构建的图像中运行以下例外:

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

geckodriver.log显示以下错误:

GTK_BACKEND doesn't match available displays

有人遇到了这一点并知道解决方法吗?它不需要访问显示器,因为它是无头运行的 - 除非冰上冰上的硒含量与常规Firefox不同?我期望类似的行为,我更喜欢使用anaconda图像

我只是尝试了这个,我几乎可以确定会解决它,但它没有起作用。

编辑:我不认为这是一个壁虎问题,因为我尝试使用firefox-esr而不是iceweasel的同一dockerfile。此外,我尝试了交互式启动容器并执行的firefox -headless(在Ubuntu上启动了无头Firefox会话(,它给出了同样的GTK错误。

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,而Firefox的-headless选项则在55版中发布。我不确定哪种版本IceWeasel是什么,但可能比这更早。另外,如果没有libgtk-3,则在52上方的Firefox版本将无效。我认为Debian 8仍然具有libgtk-2,因此也需要安装它。无头浏览可与build-essentiallibgtk-3-devwgetlibgl1-mesa-glx一起使用Debian 8。

您切勿使用软件包管理器安装Firefox。您可以在下面的链接

上找到所有Firefox版本

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

如果您查看硒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

浏览器支持

无头Firefox在Linux上的FX55 上工作,Windows/Mac上的56 。

最新更新