3

I am trying to run python selenium in docker-compose. I have the following files:

docker-compose.yml:

version: '3'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - chrome
    ports:
      - '8443:8443'

  chrome:
    image: selenium/node-chrome:3.14.0-gallium
    volumes:
      - /dev/shm:/dev/shm
    depends_on:
      - hub
    environment:
      HUB_HOST: hub

  hub:
    image: selenium/hub:3.14.0-gallium
    ports:
      - "4444:4444"

Dockerfile:

FROM    python:latest
COPY    test.py /code/test.py
WORKDIR /code
RUN     pip install --upgrade pip
RUN     pip install pytest
RUN     pip install pytest-asyncio
RUN     pip install selenium

test.py:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

driver = webdriver.Remote(
        command_executor='http://hub:4444/wd/hub',
        desired_capabilities=DesiredCapabilities.CHROME,
        )

print(driver)

I run:

docker-compose build
docker-compose run python test.py

And I get a connection-refused error in test.py when trying to create the webdriver.

'NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7ffb3b34d550>: Failed to establish a new connection: [Errno 111] Connection refused')'

Looking at the logs, the hub and the chrome driver appear to be up and running and the chrome driver is connected to the hub. I can ping the hub and chrome containers from app. Any ideas?

2 Answers 2

6

Here is a working version: Also make sure to wait for hub to be ready link to be ready before testing: https://github.com/SeleniumHQ/docker-selenium#waiting-for-the-grid-to-be-ready

version: "3.6"
services:
  selenium-hub:
    restart: always
    image: selenium/hub:3.14.0
    container_name: selenium-hub
    ports:
      - "4444:4444"

 chrome:
    restart: always
    image: selenium/node-chrome-debug:3.14.0
    ports:
      - "5900-5999:5900"
    depends_on:
      - selenium-hub
    environment:
      HUB_HOST: selenium-hub
      HUB_PORT_4444_TCP_ADDR: selenium-hub
      HUB_PORT_4444_TCP_PORT: 4444
      DBUS_SESSION_BUS_ADDRESS: "/dev/null"
    links:
      - selenium-hub:hub
Sign up to request clarification or add additional context in comments.

1 Comment

Waiting for hub to be ready was the problem. I added a loop in my python script. Every time I run, I get a few seconds of connection refused errors, then /wd/hub/status returns not ready for about a second, and then it becomes ready and works! I also found that I have to run docker-compose stop between every test or else /wd/hub/status returns not ready forever...
0

I have a very similar setup and the only difference I can see is that you have not given the HUB_PORT arg under the chrome instance:

environment:
    HUB_HOST: hub
    HUB_PORT: 4444

Example I used to get setup is here: SeleniumHQ/docker-selenium

2 Comments

Just tried this but no change. The logs show that the chrome driver successfully connects to the hub.
What is the ip you are trying to access via your test? Usually the error you mentioned appears when the endpoint you are trying to access doesn't exist/is not accessible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.