OpenQA.Selenium.WebDriverException: 'Cannot start the driver service on http://localhost:60623/'
Asked Answered
B

2

2

Following code worked well in the past. After some days, I try to run it again but it throws such error.

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

public static ChromeDriver driver;

protected void initDriver(string userDataPath) {
            var driverService = ChromeDriverService.CreateDefaultService(AppDomain.CurrentDomain.BaseDirectory);
            ChromeOptions options = new ChromeOptions();
            driverService.HideCommandPromptWindow = true;
            //options.AddArguments("--headless");
            options.AddArguments("--no-sandbox");
            options.AddArguments("disable-extensions");
            options.AddArguments("--start-minimized");

            driver = new ChromeDriver(driverService, options, TimeSpan.FromSeconds(10*60));

}

Error:

OpenQA.Selenium.WebDriverException: 'Cannot start the driver service on http://localhost:60623/'

Breebreech answered 22/6, 2019 at 5:13 Comment(0)
P
1

When the service start the only things executed are a process with the driver service and an api call to that service.
The problems that can rise could be:

  • you can't execute the process, because the executable is not reachable
    • executable not there
    • wrong permissions
  • some configurations are preventing you from executing successfully the api call and reach http://localhost:60623/
    • proxy settings (adding NO_PROXY environment variable excluding localhost might help)
    • firewall settings
    • port already used
Pogonia answered 6/5, 2020 at 13:13 Comment(2)
hi, can you show screenshot. how to config it ?Grapevine
In our case, Proxy was the issue. Thanks for sharing possible problems that can throw this exception!Lang
L
0

The main root cause of the error(Cannot start the driver service on http://localhost) is when we open multiple instances of chrome driver it assign the same port number to some of the instances and which causes this issue:

To Resolve this issue, We can assign unique port number to each instance of chrome driver and run with headless mode:

Sample Code:

IWebDriver? driver = null;
            try
            {
                int port = GetAvailablePort();
                var options = new ChromeOptions();
                options.AddArgument("headless");
                options.AddArgument($"--remote-debugging-port={port}");
                driver = new ChromeDriver(currentAssemblyLocation, options);
            }
            catch (Exception e)
            {
                if (driver != null)
                {
                    driver.Close();
                    driver.Quit();
                    driver.Dispose();
                }
                throw;
            }

 static int GetAvailableUniquePort()
        {
            TcpListener tcpListener = new(IPAddress.Loopback, 0);
            tcpListener.Start();
            int port = ((IPEndPoint)tcpListener.LocalEndpoint).Port;
            tcpListener.Stop();
            return port;
        }

Assigning a unique port with headless mode will solve the issue.

Also, make sure calling driver methods:

driver.Close(); driver.Quit();driver.Dispose();
Lang answered 18/5, 2023 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.