![](/img/trans.png)
[英]selenium webdriver error A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL
[英]An unknown exception was encountered sending an HTTP request to the remote WebDriver server
我将使用Docker
在服务器上部署我的网站。 我的组件是:
.Net 核心 API
铬驱动程序
硒集线器
使用docker-compose
我创建了我的container
,但由于此错误而无法运行:
crit: Microsoft.AspNetCore.Hosting.Diagnostics[6]
Application startup exception
OpenQA.Selenium.WebDriverException: An unknown exception was encountered sending an HTTP request to the remote WebDriver server for URL http://localhost:4444/wd/hub/session. The exception message was: Cannot assign requested address (localhost:4444)
这是docker-compose
文件:
version: "3"
services:
selenium-hub:
image: selenium/hub
container_name: selenium-hub
ports:
- "4444:4444"
chrome:
image: selenium/standalone-chrome
volumes:
- /dev/shm:/dev/shm
depends_on:
- selenium-hub
environment:
- HUB_HOST=selenium-hub
- HUB_PORT=4444
web:
build: .
ports:
- "8090:80"
这是我项目中的一段代码,我在其中实例化了driver
:
ChromeOptions options = new ChromeOptions();
options.AddArgument("no-sandbox");
options.AddArgument("headless");
driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options);
在我看来,问题源于Uri
设置,因为容器成功创建并且 selenium-hub 正在运行。
selenium-hub | 08:31:29.415 INFO [Hub.execute] - Started Selenium Hub 4.3.0 (revision a4995e2c09*): http://192.168.80.3:4444
这里的问题是您使用localhost
作为主机连接到 selenium 集线器。 当您的所有服务都作为容器在 docker 中运行时,这将不起作用。
您可以尝试在一个网络下添加所有服务,并从您的 selenium 代码中引用 selenium-hub 服务,如下所示
version: "3"
services:
chrome:
image: selenium/node-chrome:4.3.0-20220706
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
networks:
- hub_network
selenium-hub:
image: selenium/hub:4.3.0-20220706
container_name: selenium-hub
ports:
- "4442:4442"
- "4443:4443"
- "4444:4444"
networks:
- hub_network
web:
build: .
ports:
- "8090:80"
depends_on:
- selenium-hub
- chrome
restart: always
networks:
- hub_network
networks:
hub_network:
external: false
在您的硒代码中,您可以调用调用集线器,例如,
driver = new RemoteWebDriver(new Uri("http://selenium-hub:4444/wd/hub"), options);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.