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

4

10

I'm getting the following error when I try to open the EdgeDriver.

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

The FirefoxDriver and ChromeDriver work just fine.

enter image description here

This is an issue with the driver?

There is nothing running on port 20548. The below code returns nothing.

C:\WINDOWS\system32>netstat -a -o | find "20548"

Did I need to do anything else besides downlowd the Selenium.WebDriver.MicrosoftWebDriver NuGet into my project?

enter image description here

The only software I need installed on my computer is the latest version of Microsoft Edge correct? enter image description here

Birdwell answered 3/6, 2019 at 23:27 Comment(0)
B
4

I tried running MicrosoftWebDriver.exe that was in the bin directory of my project (project name, Test). This gave me the answer I needed.

enter image description here

I downloaded the correct driver from the following website:

https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/#downloads

enter image description here

========================================================

Further Update & Solution: How to know which driver to get ...

I finally found the correct driver. There are two ways to get the driver.

  1. By downloading it from the Microsoft developer website
  2. Find it in the visual studio NuGet manager and let the manger configure it into your project (preferred method)

Method 1: how to know which driver to download

  1. Go to the Microsoft developer website
  2. Find the Release which has a Release number equal to the version number on your Edge browser enter image description here
  3. Configure your project to locate and use the MicrosoftWebDriver.exe you downloaded

Method 2: how to know which driver to download

  1. Open your test project in VS and open the NuGet package manager
  2. Browse for the Selenium.WebDriver.MicrosoftWebDriver
    • Find the version of the webdriver where the last half of the version number matches the last half of your edge browser's Microsoft EdgeHTML version number (not obvious at all). enter image description here
  3. Click in install button. everything will automatically be configured in your project so you can automatically start using the driver

  4. Add the few lines of Selenium code to your test project which will open the edge browser (shown in the original question).

Birdwell answered 4/6, 2019 at 1:48 Comment(9)
Given that the NuGet package you’re referencing (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
@Ginglymus do you know if there is a NuGet for the Edge webdriver which was created by Microsoft or Selenium? If so, do you know the name of it? I looked around for one but did not find anything.Birdwell
That was rather the point of my comment, that there is no official NuGet packaging. There are not likely to be official NuGet packaging. In my opinion, using NuGet to manage random binaries (instead of referenced assemblies, its actual purpose) is an abuse of the intent of the NuGet system.Ginglymus
@JimEvans. Managing (downloading and configuring your project to reference those downloads) drivers through the NuGet manager is the taught and mainstream way to manage drivers when working with Selenium. And all discussions on working with C# Selenium will direct you to download the drivers via the VS command line package manager or the VS UI Nuget package manager. You have left me wondering since you say this is not correct yet this is the main (pretty much only) way shown/suggested for managing drivers with C# Selenium in VS.Birdwell
The “taught and mainstream way?” Taught by whom? I’m not an expert at NuGet, and don’t claim to be. I do consider myself, if not an expert, at least knowledgeable about Selenium/WebDriver on the .NET development stack. I do not prefer using NuGet for managing driver executables for the reasons I previously outlined. If that mechanism works for someone, they should use it, but I can under no circumstances consider it “correct.”Ginglymus
apologies for all the questions. I'm somewhat new to the testing world. So instead of managing the drivers through the NuGet manager (via Selenium.Driver.ChromeDriver), you instead download the driver, probably put it somewhere like the Program Files directory and then add it to your Windows Path environment variables? Similar to what is suggested in the "ADDING THE DRIVER EXECUTABLE" from this article (saucelabs.com/resources/articles/…)? Doesn't that make it difficult to transport the solution to different machines?Birdwell
And difficult for keeping track of project references via source control?Birdwell
You have many possibilities of managing driver executables. You can put them in a directory on the path, as you suggested. You can put them in a well-known location, and use the constructor overload that takes a path to the executable. You can include them in your solution and have them copied to the output directory at compile time. Any of these and more would be valid approaches.Ginglymus
Okay, I was just wondering what your preference was. That makes sense. Thanks again for the discussion.Birdwell
W
6

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

As for this issue, It could be the case that a process of the WebDriver is still running in the background. Please try to fire up Task Manager to see and end it if does.

Otherwise, you could try to use the following code to assign the webdeiver server:

        var driverpath = @"C:\Program Files (x86)\Microsoft Web Driver"; //find the web driver path
        var driver = new EdgeDriver(driverpath);
        // Navigate to Bing
        driver.Url = "https://www.bing.com/";

        // Find the search box and query for webdriver
        var element = driver.FindElementById("sb_form_q");

        element.SendKeys("webdriver");
        element.SendKeys(Keys.Enter);

        Console.ReadLine();
        driver.Quit();

This version of MicrosoftWebDriver.exe is not compatible with the installed version of Windows 10.

Please check this article to download the related Microsoft WebDriver version based on your Edge browser version.

Then, you could refer to this article to use WebDriver.

Watchman answered 4/6, 2019 at 2:4 Comment(0)
B
4

I tried running MicrosoftWebDriver.exe that was in the bin directory of my project (project name, Test). This gave me the answer I needed.

enter image description here

I downloaded the correct driver from the following website:

https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/#downloads

enter image description here

========================================================

Further Update & Solution: How to know which driver to get ...

I finally found the correct driver. There are two ways to get the driver.

  1. By downloading it from the Microsoft developer website
  2. Find it in the visual studio NuGet manager and let the manger configure it into your project (preferred method)

Method 1: how to know which driver to download

  1. Go to the Microsoft developer website
  2. Find the Release which has a Release number equal to the version number on your Edge browser enter image description here
  3. Configure your project to locate and use the MicrosoftWebDriver.exe you downloaded

Method 2: how to know which driver to download

  1. Open your test project in VS and open the NuGet package manager
  2. Browse for the Selenium.WebDriver.MicrosoftWebDriver
    • Find the version of the webdriver where the last half of the version number matches the last half of your edge browser's Microsoft EdgeHTML version number (not obvious at all). enter image description here
  3. Click in install button. everything will automatically be configured in your project so you can automatically start using the driver

  4. Add the few lines of Selenium code to your test project which will open the edge browser (shown in the original question).

Birdwell answered 4/6, 2019 at 1:48 Comment(9)
Given that the NuGet package you’re referencing (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
@Ginglymus do you know if there is a NuGet for the Edge webdriver which was created by Microsoft or Selenium? If so, do you know the name of it? I looked around for one but did not find anything.Birdwell
That was rather the point of my comment, that there is no official NuGet packaging. There are not likely to be official NuGet packaging. In my opinion, using NuGet to manage random binaries (instead of referenced assemblies, its actual purpose) is an abuse of the intent of the NuGet system.Ginglymus
@JimEvans. Managing (downloading and configuring your project to reference those downloads) drivers through the NuGet manager is the taught and mainstream way to manage drivers when working with Selenium. And all discussions on working with C# Selenium will direct you to download the drivers via the VS command line package manager or the VS UI Nuget package manager. You have left me wondering since you say this is not correct yet this is the main (pretty much only) way shown/suggested for managing drivers with C# Selenium in VS.Birdwell
The “taught and mainstream way?” Taught by whom? I’m not an expert at NuGet, and don’t claim to be. I do consider myself, if not an expert, at least knowledgeable about Selenium/WebDriver on the .NET development stack. I do not prefer using NuGet for managing driver executables for the reasons I previously outlined. If that mechanism works for someone, they should use it, but I can under no circumstances consider it “correct.”Ginglymus
apologies for all the questions. I'm somewhat new to the testing world. So instead of managing the drivers through the NuGet manager (via Selenium.Driver.ChromeDriver), you instead download the driver, probably put it somewhere like the Program Files directory and then add it to your Windows Path environment variables? Similar to what is suggested in the "ADDING THE DRIVER EXECUTABLE" from this article (saucelabs.com/resources/articles/…)? Doesn't that make it difficult to transport the solution to different machines?Birdwell
And difficult for keeping track of project references via source control?Birdwell
You have many possibilities of managing driver executables. You can put them in a directory on the path, as you suggested. You can put them in a well-known location, and use the constructor overload that takes a path to the executable. You can include them in your solution and have them copied to the output directory at compile time. Any of these and more would be valid approaches.Ginglymus
Okay, I was just wondering what your preference was. That makes sense. Thanks again for the discussion.Birdwell
P
0

on selecting multiple run setting files also, we are getting this error.

To solve this problem, we have to deselect all the selected run setting file from Test tab on VS and select any one run setting file then run your test case

Presidentelect answered 13/6, 2021 at 5:27 Comment(0)
A
0

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");
Arvid answered 17/1, 2023 at 9:2 Comment(2)
Although your answer might help other. The answer on this particular question has already been given somewhere in 2019. Therefore your answer is not the answer on the given question. since the question is already 'closed'. I think it would be better to keep you answer in mind for another occurrence where your answer seems relevant.Narial
@Narial Yes, that answer which was given in 2019 is slightly different, if anyone does not invoke their targetted methods frequently then the driver. quit() is suggestable but if you are frequently executing your methods and in the background, lots of processes are generating then my answer is suggestible because we did a whole cleanup of the process with the release of memory occupied by the processes, which impacted and prevent process deadlocks and improve system health, I hope this could be helped someone who deals with a similar issue and I will post this answer to another occurrence tooArvid

© 2022 - 2024 — McMap. All rights reserved.