Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Firefox webdriver works on images built from Ubuntu but not on images built from Debian

This is a pretty bizarre situation I have encountered. I have the following simple Python script:

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)

And wrapper for the script:

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

Additionally I have two Dockerfiles:

Dockerfile A (Ubuntu; works fine):

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 run from the image built from Dockerfile B throws the following exception:

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

The geckodriver.log shows the following error:

GTK_BACKEND doesn't match available displays

Has anyone encountered this and know a workaround? It shouldn't need access to a display because it's running headless - unless selenium Firefox options are different in iceweasel than regular Firefox? I expected similar behavior, and I would much prefer to use the Anaconda image

I just tried this which I was almost certain would solve it but it did not work.

EDIT: I do not believe this is a geckodriver issue as I tried the same Dockerfile with firefox-esr instead of iceweasel. Additionally, I tried starting the container interactively and executed firefox -headless (which on ubuntu launches a headless firefox session) it gave the same exact GTK error selenium does.

like image 225
quantik Avatar asked Feb 27 '18 21:02

quantik


1 Answers

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

Making the following changes based on what @Florent B. had linked was the fix for it. Essentially firefox-esr is version 52 and the -headless option for Firefox was released in version 55. I am not sure what version iceweasel is but presumably it is earlier than that. Additionally versions of Firefox above 52 will not work without libgtk-3. I presume Debian 8 still has libgtk-2 so one would need to install that as well. Headless browsing works perfect with build-essential, libgtk-3-dev, wget, and libgl1-mesa-glx on Debian 8.

like image 60
quantik Avatar answered Sep 20 '22 15:09

quantik