简体   繁体   English

如何解决错误文件 geckodriver.exe 正被另一个使用 Firefox 和 Selenium C# 的进程使用

[英]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#.我正在尝试使用 selenium c# 在 Chrome 和 Firefox 中运行我的测试。 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.问题是,当安装 Selenium.WebDriver.GeckoDriver 以便能够在 Firefox 浏览器上运行测试时,它会破坏我的代码,并且我无法在 chrome 或 Z763F7F1AEC350CD1C2D4623 中运行测试。 Any idea?任何想法?

I've installed我已经安装

  • Selenium.WebDriver Selenium.WebDriver
  • Selenium.WebDriver.ChromeDriver Selenium.WebDriver.ChromeDriver
  • Selenium.Firefox.WebDriver Selenium.Firefox.WebDriver
  • Selenium.WebDriver.GeckoDriver Selenium.WebDriver.GeckoDriver

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
        }

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. ...意味着先前测试执行的剩余GeckoDriver进程占用了您系统的 memory。

Unless those dangling GeckoDriver processes are removed your program would be unable to start a new GeckoDriver service.除非删除那些悬空的GeckoDriver进程,否则您的程序将无法启动新的GeckoDriver服务。

To kill the residual processes you can use the following code block:要杀死剩余进程,您可以使用以下代码块:

  • Using GetProcessesByName() :使用GetProcessesByName()

     foreach (var process in Process.GetProcessesByName("geckodriver")) { process.Kill(); }
  • Using Process.GetProcesses() filtering out the required processes:使用Process.GetProcesses()过滤掉所需的进程:

     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.理想情况下,要摆脱这个冗余过程,请始终在tearDown(){}方法中调用driver.quit()以优雅地关闭和销毁WebDriverWeb 客户端实例。


References参考

You can find a couple of relevant detailed discussions in:您可以在以下位置找到一些相关的详细讨论:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM