在docker-compose中运行python selenium

问题描述 投票:0回答:2

我试图在docker-compose中运行python selenium。我有以下文件:

码头工人,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.朋友:

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)

我跑:

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

在尝试创建webdriver时,我在test.py中收到连接拒绝错误。

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

查看日志,集线器和chrome驱动程序似乎已启动并正在运行,并且chrome驱动程序已连接到集线器。我可以从应用程序ping集线器和chrome容器。有任何想法吗?

python selenium docker docker-compose
2个回答
1
投票

这是一个工作版本:还要确保在测试之前等待集线器准备好链接准备好: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

0
投票

我有一个非常相似的设置,我可以看到的唯一区别是你没有在chrome实例下给出HUB_PORT arg:

environment:
    HUB_HOST: hub
    HUB_PORT: 4444

以前用于设置的示例在这里:SeleniumHQ/docker-selenium

© www.soinside.com 2019 - 2024. All rights reserved.