What's the right way to pass browser profile to selenium inside docker container?

Viewed 729

I need to launch selenium inside docker container. It's important to pass browser profile to webdriver.

Here's docker-compose:

version: '2'

services:
  worker_main:
    build: ./app
    volumes:
      - /Users/username/Library/Application Support/Google/Chrome/Profile 1:/profile
    restart: always
    env_file:
      - config.env
    networks:
      - backend
    depends_on:
      - chrome

  chrome:
    image: selenium/standalone-chrome
    restart: always
    ports:
      - 4444:4444
    hostname: chrome
    networks:
      - backend

networks:
  backend:

Here's driver code:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=/profile")

driver = webdriver.Remote("http://chrome:4444/wd/hub", options=options)

As a result I catch this error:

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot create default profile directory

1 Answers

You can try doing it this way:

$ docker run --rm -p 4444:4444 -p 5900:5900 \
  -v /tmp/chrome_profiles:/tmp/chrome_profiles \
  -e JAVA_OPTS selenium/standalone-chrome:latest

or

# To execute this docker-compose yml file use `docker-compose -f <file_name> up`
# Add the `-d` flag at the end for detached execution
version: '2'
services:
  chrome:
    image: selenium/node-chrome:latest
    volumes:
      - /dev/shm:/dev/shm
      - /tmp/chrome:/tmp/chrome_profiles
    ports:
      - "5900:5900"
    depends_on:
      - hub
    environment:
      HUB_HOST: hub

  hub:
    image: selenium/hub:latest
    ports:
      - "4444:4444"

The profile path then needs to be passed through ChromeOptions, keep in mind that this is the path inside the container. Example code:

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("-profile", "/tmp/chrome_profiles/<replace_with_profile_id>.selenium");

WebDriver driver = new RemoteWebDriver(new URL("http://hub:4444/wd/hub"), chromeOptions);
Related