如何在docker容器(python)中运行selenium



当前正在尝试学习docker。似乎无法使硒在容器内发挥作用。

我的docker撰写文件看起来像这个

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之后,它将运行没有问题的

我有什么东西不见了吗?

您的服务的名称是"集线器";所以试着用正确的主机运行你的代码,如下所示:

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

最新更新