python - 在 docker-compose 中运行 python selenium
问题描述
我正在尝试在 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
测试.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
推荐阅读
- mediawiki - 如何等待 Wikibase 编辑链接完成加载
- python - 如何在 matplotlib + PyQT5 中激活 WebGL 流?
- c# - 通过 x,y 识别所选按钮的最佳方法是什么
- datatables - DataTables 添加 aria 角色和 aria 扩展为详细信息控件(加号/减号按钮)
- apache-kafka - kafka流客户端能否以编程方式确定其消费者滞后
- r - knitr 在文档中生成错误,但无论如何都会正确生成数字
- docker - getsockopt 异常失败:Docker-Compose 中不允许操作
- python - 尝试使用 Django 在 Apache Server 上创建 pdf 文件时出现权限问题
- node.js - 手动将 aws-sdk 复制到 nodejs
- tensorflow - 神经网络权重太大?