gpt4 book ai didi

python - 在 docker-compose 中运行 python selenium

转载 作者:行者123 更新时间:2023-12-02 18:09:31 27 4
gpt4 key购买 nike

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

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"

docker 文件:

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

测试.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)

我跑:

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 容器。有什么想法吗?

最佳答案

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

关于python - 在 docker-compose 中运行 python selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54358026/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com