Home > Net >  An unknown exception was encountered sending an HTTP request to the remote WebDriver server
An unknown exception was encountered sending an HTTP request to the remote WebDriver server

Time:07-21

I am going to deploy my website on a server with the use of Docker. My components are:

.Net Core Api

Chrome Driver

Selenium Hub

With the usage of docker-compose I created my container but it could not be run because of this error:

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)

This is the docker-compose file:

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"

And this a piece of code in my project in which I instantiate the driver:

    ChromeOptions options = new ChromeOptions();
    options.AddArgument("no-sandbox");
    options.AddArgument("headless");

    driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options);

In my opinion the problem stem from Uri setting because the container successfully created and selenium-hub is running.

selenium-hub      | 08:31:29.415 INFO [Hub.execute] - Started Selenium Hub 4.3.0 (revision a4995e2c09*): http://192.168.80.3:4444

CodePudding user response:

The issue here is you are using localhost as host to connect to selenium hub. That would not work when all your services are running as containers in docker.

what you can try is add all your services under one network and reference to the selenium-hub service from your selenium code as shown below

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

And in your selenium code you can call invoke the hub like,

driver = new RemoteWebDriver(new Uri("http://selenium-hub:4444/wd/hub"), options);
  • Related