Armin Mesicpersonal blog

This tutorial covers how to setup and debug Selenium with Docker, it can be used to automate testing or to crawl through websites. I’ll be using Python but it should work the same way for different languages too.

First we need to create adocker-compose.ymlfile, it will launch our Selenium instance,docker-compose.ymlshould contain following lines

version: '3'

services:
  tutorial-selenium:
    image:  "selenium/standalone-chrome-debug"
    container_name: tutorial-selenium
    volumes:
      - /dev/shm:/dev/shm
    ports:
    - 5900:5900

  tutorial-python:
    image: "python"
    container_name: tutorial-python
    tty: true
    depends_on:
      - tutorial-selenium
    volumes:
    - ./code:/code

Quick explanation what is happening. We’re using theselenium/standalone-chrome-debug, this makes debugging easier for us because it allows us to connect with a VNC client and see what is happening. Port5900is being exposed because we’re going to use it to connect with our VNC client. Download a VNC client of your choice and connect tolocalhost:5000with the default passwordsecret.

/dev/shmis mounted to use the hosts share memory or else instances of Chrome or Firefox would crash, you can read more about theChrome issueand theFirefox issue.

The python container mounts thecodefolder which contains our code.tty:truekeeps the container running to make debugging the python code easier.

Next step we’re going to write our code to connect with Selenium. First we need to addseleniumto ourrequirements.txtfile, so create a filecode/requirements.txtand addselenium. Next create a new filecode/app.py, it will contain main code.

from selenium import webdriver

driver = webdriver.Remote(
    desired_capabilities=webdriver.DesiredCapabilities.CHROME,
    command_executor='http://tutorial-selenium:4444/wd/hub'
)
driver.get('https://google.com')
print(f"Page title: {driver.title}")
driver.quit()

This code opens a page (https://google.com) and prints the page title. We’re connection to a remote instance of Chrome which is reachable withhttp://tutorial-selenium-chrome:4444/wd/hubfrom within the container. To see it in action rundocker exec -it tutorial-python bash, once you’ve established a connection to the container runcd /code. Now you need to install the dependencies withpip install -r requirements.txtand finally you can execute the codepython app.py. If you’re using a VNC client and have created a connection withlocalhost:5900(password issecret) you should see a browser opening and visitinghttps://google.comand closing again.

Now you can adjust your code to do the things you need to, when you’re done and you application is running successfully you can update yourdocker-compose.ymlfile and change the debug version of selenium with a normal one.

services:
  tutorial-selenium:
    image:  "selenium/standalone-chrome"
    container_name: tutorial-selenium
    volumes:
      - /dev/shm:/dev/shm

  tutorial-python:
    image: "python"
    container_name: tutorial-python
    tty: true
    depends_on:
      - tutorial-selenium
    volumes:
    - ./code:/code

I’ve used Chrome for our example, but this can be done also for Firefox, you just need to change the images,selenium/standalone-chrome-debugbecomesselenium/standalone-firefox-debug,selenium/standalone-chromebecomesselenium/standalone-firefoxand finally you need to updateapp.pyto use Firefox.

from selenium import webdriver

driver = webdriver.Remote(
    desired_capabilities=webdriver.DesiredCapabilities.FIREFOX,
    command_executor='http://tutorial-selenium:4444/wd/hub'
)
driver.get('https://google.com')
print(f"Page title: {driver.title}")
driver.quit()

If there are any open questions you can send a mail, it’s provided on the about me page.