Skip to content

Commit

Permalink
feat(core): project init
Browse files Browse the repository at this point in the history
  • Loading branch information
dimmg committed Oct 2, 2017
0 parents commit 2ca3dca
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
FROM ubuntu:trusty

RUN apt-get update

RUN apt-get install -y python3 python3-pip

RUN apt-get install -y libgconf2-4 libnss3-1d libxss1
RUN apt-get install -y fonts-liberation libappindicator1 xdg-utils

RUN apt-get install -y software-properties-common
RUN apt-get install -y curl unzip wget

RUN apt-get install -y xvfb


# install geckodriver and firefox

RUN GECKODRIVER_VERSION=`curl https://github.com/mozilla/geckodriver/releases/latest | grep -Po 'v[0-9]+.[0-9]+.[0-9]+'` && \
wget https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz && \
tar -zxf geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz -C /usr/local/bin && \
chmod +x /usr/local/bin/geckodriver

RUN add-apt-repository -y ppa:ubuntu-mozilla-daily/ppa
RUN apt-get update -y
RUN apt-get install -y firefox


# install chromedriver and google-chrome

RUN CHROMEDRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \
wget https://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip
RUN unzip chromedriver_linux64.zip -d /usr/bin
RUN chmod +x /usr/bin/chromedriver

RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome*.deb
RUN apt-get install -y -f


RUN pip3 install selenium
RUN pip3 install pyvirtualdisplay


ENV APP_HOME /usr/src/app
WORKDIR /$APP_HOME

COPY . $APP_HOME/

CMD tail -f /dev/null
# CMD python3 example.py
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## dockselpy

Dockerfile example on how to *"assemble"* together Selenium (with support for both Chrome and Firefox), Python and Xfvb.

### Information

Recent struggle with finding a docker image for Selenium that supports headless versions for both Firefox and Chrome,
led to the process of building my own version.

The image is build with the following dependencies:
- latest Chrome and chromedriver
- latest Firefox and geckodriver
- Selenium
- Python 3
- Xvfb and the python wrapper - pyvirtualdisplay


### Running:

- docker
```
docker build -t selenium_docker .
docker run --privileged -p 4000:4000 -d -it selenium_docker
```
- docker-compose
```
docker-compose stop && docker-compose build && docker-compose up -d
```
### Example
```python
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Firefox()
browser.get('https://www.google.com/')
print(browser.title)
browser.quit()
display.stop()
```

Detailed examples on how to use Firefox with custom profile and Google Chrome with desired options can be found in the
source.
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
selenium:
build: .
ports:
- 4000:4000
privileged: true
65 changes: 65 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os
import logging

from pyvirtualdisplay import Display
from selenium import webdriver

logging.getLogger().setLevel(logging.INFO)

BASE_URL = 'http://www.example.com/'


def chrome_example():
display = Display(visible=0, size=(800, 600))
display.start()
logging.info('Initialized virtual display..')

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')

chrome_options.add_experimental_option('prefs', {
'download.default_directory': os.getcwd(),
'download.prompt_for_download': False,
})
logging.info('Prepared chrome options..')

browser = webdriver.Chrome(chrome_options=chrome_options)
logging.info('Initialized chrome browser..')

browser.get(BASE_URL)
logging.info('Accessed %s ..', BASE_URL)

logging.info('Page title: %s', browser.title)

browser.quit()
display.stop()


def firefox_example():
display = Display(visible=0, size=(800, 600))
display.start()
logging.info('Initialized virtual display..')

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference('browser.download.folderList', 2)
firefox_profile.set_preference('browser.download.manager.showWhenStarting', False)
firefox_profile.set_preference('browser.download.dir', os.getcwd())
firefox_profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv')

logging.info('Prepared firefox profile..')

browser = webdriver.Firefox(firefox_profile=firefox_profile)
logging.info('Initialized firefox browser..')

browser.get(BASE_URL)
logging.info('Accessed %s ..', BASE_URL)

logging.info('Page title: %s', browser.title)

browser.quit()
display.stop()


if __name__ == '__main__':
chrome_example()
firefox_example()

0 comments on commit 2ca3dca

Please sign in to comment.