This exception "OpenQA.Selenium.WebDriverException: 'Cannot start the driver service" can be occurred because of processes (i.e. chrome, chromeDriver) are still running in the background.
We faced a similar kind of issue and we resolved in Selenium with C# Test project
A) you can use all the process/instances termination methods one after one Close(), Quit(), Dispose()
if you are not frequently executing test methods/specific code!
but still, the exception is showing after the use of the above things then you can try the following solution (C# code)
(We used the class "ManagementObjectResearcher" which is used for managing OS-related things like hard drivers, external devices, and processes, by using this class you can supply WQL queries and locate all the processes with process id and terminate all the processes with their processes id which will overcome the problem of process deadlock)
//Method overloading - To terminate process with releasing memory with reference of PIDpublic void KillProcess(string p_name)
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher
("Select * From Win32_Process Where Name = '" + p_name + "'");
ManagementObjectCollection moc = searcher.Get();
foreach (ManagementObject mo in moc)
{
try
{
KillProcess(Convert.ToInt32(mo["ProcessID"]));
}
catch (ArgumentException)
{
break;
}
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
//locate each and every PID
public void KillProcess(int pid)
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher
("Select * From Win32_Process Where ParentProcessID=" + pid);
ManagementObjectCollection moc = searcher.Get();
foreach (ManagementObject mo in moc)
{
try
{
KillProcess(Convert.ToInt32(mo["ProcessID"]));
}
catch
{
break;
}
}
try
{
Process proc = Process.GetProcessById(pid);
proc.Kill();
}
catch (ArgumentException)
{
// Process already exited.
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
and use following in Teardown
KillProcess("chrome.exe");
Thread.Sleep(2000);
KillProcess("chromedriver.exe");
Selenium.WebDriver.MicrosoftWebDriver
) is maintained by neither the Selenium project, nor Microsoft, I would be careful about trusting that the package is properly maintained. Moreover, since the situation is similar for all other browser-specific driver executables, I’d hesitate to call managing driver executables via NuGet “preferred” as a general rule. – Ginglymus