├── .gitignore ├── Dockerfile ├── README.md ├── chrome_test.py ├── chrome_test_xfvb.py ├── chromedriver ├── gecko_test.py └── geckodriver /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .cache 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:buster 2 | MAINTAINER Rafal Fusik 3 | 4 | USER root 5 | RUN \ 6 | apt-get update \ 7 | && echo '**** Set up python **** ' \ 8 | && apt-get install -y python python-dev python-distribute python-pip \ 9 | && pip install pyvirtualdisplay \ 10 | && pip install xvfbwrapper \ 11 | && echo '**** Set up selenium, pytest **** ' \ 12 | && pip install -U selenium \ 13 | && pip install -U pytest \ 14 | && echo 'Setting up xvfb ...' \ 15 | && apt-get -y install xvfb 16 | 17 | RUN \ 18 | echo '**** Setup firefox **** ' \ 19 | && apt-get remove iceweasel \ 20 | && apt-get -y install apt-transport-https ca-certificates \ 21 | && echo '\ndeb http://downloads.sourceforge.net/project/ubuntuzilla/mozilla/apt all main' | tee -a /etc/apt/sources.list > /dev/null \ 22 | && apt-key adv --recv-keys --keyserver keyserver.ubuntu.com C1289A29 \ 23 | && apt-get update \ 24 | && apt-get install firefox-mozilla-build \ 25 | && apt-get install -y libdbus-glib-1-2 \ 26 | && apt-get install -y libgtk2.0-0 \ 27 | && apt-get install -y libasound2 28 | 29 | RUN \ 30 | echo '**** Create test directory **** ' \ 31 | && mkdir /usr/local/test 32 | 33 | RUN \ 34 | apt-get install -y unzip libxi6 libgconf-2-4 35 | 36 | RUN \ 37 | echo '**** Install chrome **** ' \ 38 | && apt-get install -y wget \ 39 | && apt -y --fix-broken install \ 40 | && apt-get -y install libxss1 libappindicator1 libindicator7 \ 41 | && wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb \ 42 | && dpkg -i google-chrome*.deb; exit 0 43 | 44 | RUN apt-get -y install -f && \ 45 | apt-get -y install unzip 46 | 47 | RUN \ 48 | echo '**** Copy gecko & chromedriver ****' 49 | 50 | COPY geckodriver /usr/local/bin 51 | COPY chromedriver /usr/local/bin 52 | 53 | RUN \ 54 | echo '**** Copy tests ****' 55 | 56 | COPY gecko_test.py /usr/local/test 57 | COPY chrome_test_xfvb.py /usr/local/test 58 | COPY chrome_test.py /usr/local/test 59 | 60 | RUN \ 61 | echo 'All went well Done!' 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### update drivers (current: geckodriver-v0.19.0 , chrome-2.33) 2 | [geckodriver releases](https://github.com/mozilla/geckodriver/releases) 3 | [chromedriver releases](https://sites.google.com/a/chromium.org/chromedriver/downloads) 4 | 5 | ### Cleaning local docker repository 6 | 7 | ```bash 8 | docker ps -a | egrep "[Ee]xited" | awk '{print $1}' | xargs -Izz55 docker rm zz55 9 | docker images | sed -E "s/[[:space:]]+/ /g" | awk '{print $3}' | xargs -Izz55 docker rmi zz55 10 | docker volume ls | sed -E "s/[[:space:]]+/ /g" | awk '{print $2}' | xargs -Izz55 docker volume rm zz55 11 | ``` 12 | 13 | ### Build 14 | ```bash 15 | docker build -t debian-headless . 16 | ``` 17 | 18 | ### Running an interactive shell 19 | ```bash 20 | docker run -it debian-headless /bin/bash 21 | ``` 22 | 23 | ### Chrome tests: 24 | 1. xfvb 25 | ``` 26 | chrome_test_xfvb.py 27 | ``` 28 | 29 | ```python 30 | from selenium import webdriver 31 | from selenium.webdriver.common.keys import Keys 32 | from selenium.webdriver.chrome.options import Options 33 | import xvfbwrapper 34 | 35 | # Start headless display 36 | display = xvfbwrapper.Xvfb() 37 | display.start() 38 | print('display started') 39 | 40 | chrome_options = Options() 41 | chrome_options.add_argument('--no-sandbox') 42 | chrome_options.add_argument("--disable-gpu") 43 | driver = webdriver.Chrome(chrome_options=chrome_options) 44 | driver.get("http://www.google.com") 45 | print(driver.title) 46 | driver.quit() 47 | display.stop() 48 | print('display stopped') 49 | ``` 50 | 2. --headless 51 | ``` 52 | chrome_test.py 53 | ``` 54 | 55 | ```python 56 | from selenium import webdriver 57 | from selenium.webdriver.common.keys import Keys 58 | from selenium.webdriver.chrome.options import Options 59 | 60 | chrome_options = Options() 61 | chrome_options.add_argument("--headless") 62 | chrome_options.add_argument('--no-sandbox') 63 | chrome_options.add_argument("--disable-gpu") 64 | driver = webdriver.Chrome(chrome_options=chrome_options) 65 | driver.get("http://www.google.com") 66 | print(driver.title) 67 | driver.quit() 68 | ``` 69 | 70 | ### Resources: 71 | [debian library](https://docs.docker.com/samples/library/debian/) 72 | [python-headless-chromedriver](https://github.com/rsanchezavalos/python-headless-chromedriver) _fail out of the box_ 73 | [firefox-headless](http://scraping.pro/use-headless-firefox-scraping-linux/) 74 | [docker ignore error](https://stackoverflow.com/questions/30716937/dockerfile-build-possible-to-ignore-error) 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /chrome_test.py: -------------------------------------------------------------------------------- 1 | from selenium import webdriver 2 | from selenium.webdriver.common.keys import Keys 3 | from selenium.webdriver.chrome.options import Options 4 | 5 | chrome_options = Options() 6 | chrome_options.add_argument("--headless") 7 | chrome_options.add_argument('--no-sandbox') 8 | driver = webdriver.Chrome(chrome_options=chrome_options) 9 | driver.get("http://www.google.com") 10 | print(driver.title) 11 | driver.quit() 12 | -------------------------------------------------------------------------------- /chrome_test_xfvb.py: -------------------------------------------------------------------------------- 1 | from selenium import webdriver 2 | from selenium.webdriver.common.keys import Keys 3 | from selenium.webdriver.chrome.options import Options 4 | import xvfbwrapper 5 | 6 | # Start headless display 7 | display = xvfbwrapper.Xvfb() 8 | display.start() 9 | print('display started') 10 | 11 | chrome_options = Options() 12 | chrome_options.add_argument('--no-sandbox') 13 | driver = webdriver.Chrome(chrome_options=chrome_options) 14 | driver.get("http://www.google.com") 15 | print(driver.title) 16 | driver.quit() 17 | display.stop() 18 | print('display stopped') 19 | -------------------------------------------------------------------------------- /chromedriver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafalf/docker-debian-selenium-headless/e2fa905b455bc9ed0298ee1fede14d6caa3a439a/chromedriver -------------------------------------------------------------------------------- /gecko_test.py: -------------------------------------------------------------------------------- 1 | from pyvirtualdisplay import Display 2 | from selenium import webdriver 3 | 4 | display = Display(visible=0, size=(800, 600)) 5 | display.start() 6 | 7 | browser = webdriver.Firefox() 8 | browser.get('http://www.google.com') 9 | print(browser.title) 10 | browser.quit() 11 | 12 | display.stop() 13 | print('test stopped, sucess!') 14 | -------------------------------------------------------------------------------- /geckodriver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafalf/docker-debian-selenium-headless/e2fa905b455bc9ed0298ee1fede14d6caa3a439a/geckodriver --------------------------------------------------------------------------------