gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:31:47 24 4
gpt4 key购买 nike

这是我遇到的一个非常奇怪的情况。我有以下简单的 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:消息:进程意外关闭,状态为:1

geckodriver.log 显示以下错误:

GTK_BACKEND 与可用显示不匹配

有没有人遇到过这个并且知道解决方法?它不需要访问显示器,因为它是 headless 运行的——除非 iceweasel 中的 selenium Firefox 选项与普通 Firefox 不同?我预计会有类似的行为,而且我更愿意使用 Anaconda 图像

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

编辑:我不认为这是 geckodriver 问题,因为我用 firefox-esr 而不是 iceweasel 尝试了相同的 Dockerfile。此外,我尝试以交互方式启动容器并执行 firefox -headless(在 ubuntu 上启动 headless firefox session )它给出了与 selenium 完全相同的 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,因此也需要安装它。 headless 浏览与 build-essentiallibgtk-3-devwgetlibgl1-mesa-glx 完美配合> 在 Debian 8 上。

关于python - Selenium Firefox webdriver 适用于从 Ubuntu 构建的图像,但不适用于从 Debian 构建的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49018497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com