我将使用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正在运行。
selenium-hub | 08:31:29.415 INFO [Hub.execute] - Started Selenium Hub 4.3.0 (revision a4995e2c09*): http://192.168.80.3:4444发布于 2022-07-21 09:53:00
这里的问题是您正在使用localhost作为主机连接到selenium集线器。当您的所有服务都作为集装箱在码头上运行时,这是行不通的。
您可以尝试的是在一个网络中添加所有服务,并从selenium代码中引用selenium-集线器服务,如下所示
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在您的selenium代码中,您可以调用集线器,
driver = new RemoteWebDriver(new Uri("http://selenium-hub:4444/wd/hub"), options);https://stackoverflow.com/questions/73063505
复制相似问题