【问题标题】:How to run selenium inside docker container (python)如何在 docker 容器(python)中运行 selenium
【发布时间】:2021-06-14 00:42:00
【问题描述】:

目前正在努力学习 docker。似乎无法让硒在容器内工作。

我的 docker compose 文件是这样的

version: '3.8'

services:

  hub:
    image: selenium/hub:3.141.59
    ports:
      - 4444:4444

  chrome:
    image: selenium/node-chrome:3.141.59
    depends_on:
      - hub
    environment:
      - HUB_HOST=hub

  firefox:
    image: selenium/node-firefox:3.141.59
    depends_on:
      - hub
    environment:
      - HUB_HOST=hub

  app:
    build:
      context: ./
    volumes:
      - ./app:/app
    command: "python main.py"
    depends_on:
      - chrome

我的 dockerfile 看起来像

FROM python:3.9

COPY ./requirements.txt requirements.txt
RUN pip install -r /requirements.txt

RUN mkdir /app
COPY ./app /app
WORKDIR /app

EXPOSE 4444

我尝试了很多方法,但目前我的驱动看起来像

caps = {'browserName': os.getenv('BROWSER', 'chrome')}
self.driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub',
                               desired_capabilities=caps)

运行后

docker-compose build
docker-compose up

无论我似乎改变了什么,我似乎总是得到

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

如果我只是从我的终端运行 main.py 之后它将没有问题

我有什么遗漏吗?

【问题讨论】:

    标签: python docker selenium docker-compose dockerfile


    【解决方案1】:

    您的服务名称是“hub”,因此请尝试使用正确的主机运行您的代码,如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多