How to use Chrome with Selenium inside a Docker container running Python

2017-08-13

Running a Chrome browser inside a Docker container with Selenium used to be a challenging thing. Since Chrome 59 shipped with a headless mode, this has been made much easier.

First, you need Google Chrome and the chromedriver installed.

To find the file to install Google Chrome, go to https://www.google.com/chrome/ and get the URL to the latest version for Linux 64 bits.

To find the file to install chromedriver, go to https://sites.google.com/a/chromium.org/chromedriver/downloads and get the URL for the latest Linux 64 bits version.

In a Dockerfile, ideally, everything should be frozen, so you know updates won't break the build process. What I do is I download the files and save them in a S3 bucket, and use the S3 URL in the Dockerfile. In this tutorial, I won't do that, so remember that if you want to freeze the versions by doing that, you have to replace the URLs.

Create a Dockerfile:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
FROM ubuntu:17.04

# Install Chrome for Selenium
RUN curl https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -o /chrome.deb
RUN dpkg -i /chrome.deb || apt-get install -yf
RUN rm /chrome.deb

# Install chromedriver for Selenium
RUN curl https://chromedriver.storage.googleapis.com/2.31/chromedriver_linux64.zip -o /usr/local/bin/chromedriver
RUN chmod +x /usr/local/bin/chromedriver

Now you can run any code that uses Selenium with Chrome in the container. Here's a class I wrote to instantiate the browser and use it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


class WebDriver:
    DOWNLOAD_DIR = '/tmp'

    def __init__(self, headless=True):
        self.options = webdriver.ChromeOptions()

        self.options.add_argument('--disable-extensions')
        if headless:
            self.options.add_argument('--headless')
            self.options.add_argument('--disable-gpu')
            self.options.add_argument('--no-sandbox')

        self.options.add_experimental_option(
            'prefs', {
                'download.default_directory': self.DOWNLOAD_DIR,
                'download.prompt_for_download': False,
                'download.directory_upgrade': True,
                'safebrowsing.enabled': True
            }
        )

    def __enter__(self):
        self.open()
        return self

    def __exit__(self, *args, **kwargs):
        self.close()

    def open(self):
        self.driver = webdriver.Chrome(chrome_options=self.options)
        self.driver.implicitly_wait(10)

    def close(self):
        self.driver.quit()

    def login(self):
        # Change this function to your needs and add other functions, etc...
        self.driver.get('http://www.test.com')
        username_field = self.driver.find_element_by_id('username')
        username_field.clear()
        username_field.send_keys('username')
        username_field.send_keys(Keys.RETURN)
        assert 'Login successful' in driver.page_source
        self.driver.close()

And this is how you use it:

1
2
3
4
from driver import WebDriver

with WebDriver() as driver:
    driver.login()

If you are testing the code in your local machine with a GUI, then just pass headless=False to WebDriver and the browser will open a window and show you how it interacts with the pages. Very useful for debugging.