【发布时间】: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