Yorda
Yorda

Reputation: 15

How to address the error The file geckodriver.exe is being used by another process using Firefox and Selenium C#

I'm trying to run my test in Chrome and Firefox using selenium c#. The problem is, when install the Selenium.WebDriver.GeckoDriver to be able to run the test on Firefox browser it breaks my code and I'm not able to run the test in chrome or Firefox. Any idea?

I've installed

Error:

The file geckodriver.exe is being used by another process   

Another error:

The file is locked by geckodriver

Code trials:

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using System;

    internal class Program
        {
            IWebDriver driver = new ChromeDriver();
            //IWebDriver driver = new FirefoxDriver();
            static void Main(string[] args)
            {
            }
            [SetUp] //method to initialize page with windows maximized. 
            public void Initialize()
            {
                driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(30);
                driver.Navigate().GoToUrl("https://www.demo.bnz.co.nz/client/");
                driver.Manage().Window.Maximize();
                driver.Manage().Cookies.DeleteAllCookies();
            }
public void Payees()
        {
            System.Threading.Thread.Sleep(10000); //verify if page is loaded 
            IWebElement element = driver.FindElement(By.XPath("//*[@id='left']/div[1]/div/button"));//menu            
            element.Click();
            driver.FindElement(By.XPath("//*[@id='left']/div[1]/div/div[3]/section/div[2]/nav[1]/ul/li[3]/a")).Click(); //payes
        }

Upvotes: 0

Views: 900

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193108

This error message...

"The file geckodriver.exe is being used by another process

and

The file is locked by geckodriver

...implies that there are residual GeckoDriver processes of the previous test execution occupying your system's memory.

Unless those dangling GeckoDriver processes are removed your program would be unable to start a new GeckoDriver service.

To kill the residual processes you can use the following code block:

  • Using GetProcessesByName():

    foreach (var process in Process.GetProcessesByName("geckodriver"))
    {
        process.Kill();
    }
    
  • Using Process.GetProcesses() filtering out the required processes:

    var chromeDriverProcesses = Process.GetProcesses();
        Where(pr => pr.ProcessName == "chromedriver"); // without .exe
    
    foreach (var process in chromeDriverProcesses)
    {
       process.Kill();
    }
    

Ideal Solution

Ideally to get rid of this redundant process always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.


References

You can find a couple of relevant detailed discussions in:

Upvotes: 1

Related Questions