【问题标题】:Selenium Firefox webdriver works on images built from Ubuntu but not on images built from DebianSelenium Firefox webdriver 适用于从 Ubuntu 构建的映像,但不适用于从 Debian 构建的映像
【发布时间】:2018-02-27 21:49:16
【问题描述】:

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

另外我有两个 Dockerfile:

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;崩溃):

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

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

我刚刚尝试了this,我几乎可以肯定它会解决它,但它不起作用。

编辑:我不认为这是壁虎驱动程序问题,因为我尝试使用 firefox-esr 而不是 iceweasel 使用相同的 Dockerfile。此外,我尝试以交互方式启动容器并执行firefox -headless(在 ubuntu 上启动无头 firefox 会话)它给出了与 selenium 完全相同的 GTK 错误。

【问题讨论】:

  • 无头模式是在 Linux 55 版本中引入的,当前 Firefox 的 ESR 版本是 52。我猜-headless 标志被忽略了,Firefox 由于缺少显示而崩溃。尝试使用更新的版本:github.com/SeleniumHQ/docker-selenium/blob/master/NodeFirefox/…
  • @FlorentB。这是个好主意,我会试试看
  • @FlorentB。这行得通!排序。您需要对 Docker 和 Debian 进行一些修改。你不能apt-get install firefox 因为Debian 没有firefox 包。此外,您必须安装libgtk-3-dev。这会自动安装在ubuntu:latest 上,但不会自动安装在debian:8 上。进行这些更改,我会接受您的回答。

标签: python selenium docker debian iceweasel


【解决方案1】:
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,所以也需要安装它。无头浏览与 Debian 8 上的 build-essentiallibgtk-3-devwgetlibgl1-mesa-glx 完美搭配。

【讨论】:

  • 用 debian:stretch 和上面的脚本我得到XPCOMGlueLoad error for file /firefox/libxul.so: libdbus-glib-1.so.2: cannot open shared object file: No such file or directory Couldn't load XPCOM.
  • 运行 apt-get install -y libdbus-glib-1-2 修复它
  • 任何人都可以访问此链接。这很有帮助。 github.com/SeleniumHQ/docker-selenium/blob/trunk/NodeFirefox/…
【解决方案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+。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多