chu*_*ck1 2 python selenium docker docker-compose
我正在尝试在 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"
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
测试.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)
Run Code Online (Sandbox Code Playgroud)
我跑:
docker-compose build
docker-compose run python test.py
Run Code Online (Sandbox Code Playgroud)
尝试创建 webdriver 时,我在 test.py 中收到连接拒绝错误。
'NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7ffb3b34d550>: Failed to establish a new connection: [Errno 111] Connection refused')'
Run Code Online (Sandbox Code Playgroud)
查看日志,集线器和 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4195 次 |
| 最近记录: |