wookietreiber
wookietreiber

Reputation: 1

Execute Selenium Python Script within Docker

I am trying to run a Selenium script written in Python inside a Docker container via Selenium Grid. Unfortunately I can't manage to configure the remote webdriver.

This is the Docker Compose file:

version: "3"
services:
  chrome:
    image: selenium/node-chrome:4.1.3-20220327
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  firefox:
    image: selenium/node-firefox:4.1.3-20220327
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  selenium-hub:
    image: selenium/hub:4.1.3-20220327
    container_name: selenium-hub
    ports:
      - "4444:4444"

  python-script:
    build: .

This is the webdriver setup within the Python code:

    driver = webdriver.Remote(
        desired_capabilities=DesiredCapabilities.FIREFOX,
        command_executor="http://localhost:4444/wd/hub"
    )
        

It works when I run the Python script locally with these settings. But as soon as I want to start it inside a Docker container, I get the following error, among others:

urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=4444): Max retries exceeded with url: /session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f7b85c41780>: Failed to establish a new connection: [Errno 111] Connection refused'))

I'm totally new to docker and also quite new to programming itself, so help would be very very nice.

Thank you!

Upvotes: 0

Views: 2077

Answers (1)

Jens Tandstad
Jens Tandstad

Reputation: 41

TLDR : Try this:

driver = webdriver.Remote(
    desired_capabilities=DesiredCapabilities.FIREFOX,
    command_executor="http://selenium-hub:4444/wd/hub"
)

The reason it works locally from vs-code, is that localhost points to your local machine. Your docker container has its own idea of what localhost means. When the code runs inside the container, localhost refers to that container. Is that container listening to that port? Probably not - and this is why it doesn't work. Docker has its own network stack!

What you want to contact is the other container "selenium-hub". In docker, the service-name (or container name) becomes the host - but this only works from within the docker network. (Docker-compose makes a default network for you you don't specify one)

Upvotes: 1

Related Questions