【发布时间】:2022-11-10 17:52:14
【问题描述】:
我正在尝试通过 Selenium Grid 在 Docker 容器内运行用 Python 编写的 Selenium 脚本。不幸的是,我无法配置远程 webdriver。
这是 Docker 撰写文件:
version: "3"
services:
chrome:
image: selenium/node-chrome:4.1.3-20220327
shm_size: 2gb
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
firefox:
image: selenium/node-firefox:4.1.3-20220327
shm_size: 2gb
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
selenium-hub:
image: selenium/hub:4.1.3-20220327
container_name: selenium-hub
ports:
- "4444:4444"
python-script:
build: .
这是 Python 代码中的 webdriver 设置:
driver = webdriver.Remote(
desired_capabilities=DesiredCapabilities.FIREFOX,
command_executor="http://localhost:4444/wd/hub"
)
当我使用这些设置在本地运行 Python 脚本时,它可以工作。但是,一旦我想在 Docker 容器中启动它,就会收到以下错误,其中包括:
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=4444): Max retries exceeded with url: /session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f7b85c41780>: Failed to establish a new connection: [Errno 111] Connection refused'))
我对 docker 完全陌生,对编程本身也很陌生,所以帮助会非常非常好。
谢谢!
【问题讨论】:
-
此错误通常意味着您的互联网太慢或无法正常工作,或者您有某种防火墙阻止了请求。我一直用
pip得到它。 -
@SylvesterKruin 的评论让我深思熟虑。不是互联网太慢,而是 Docker 容器内的浏览器服务设置比我的脚本慢。添加适当的依赖项解决了它。谢谢!
标签: python docker selenium firefox docker-compose