【问题标题】:ChromeDriver does not exist in Selenium WebDriver C# test scriptSelenium WebDriver C# 测试脚本中不存在 ChromeDriver
【发布时间】:2021-02-12 21:34:39
【问题描述】:

在实例化驱动程序之前,我遇到过一些人似乎已经解决了 System.addProperty("webdriver.chrome.driver", ".../chromedriver.exe"); 的问题。

我对此运气不佳,但仍然收到文件.../bin/Debug/chromedriver.exe 不存在的错误。

有没有人在不将其放入 bin 文件夹的情况下运行它?

示例代码:

System.Environment.SetEnvironmentVariable("webdriver.chrome.driver", @"c:\path\to\driver\chromedriver.exe");
BrowserDriver = new ChromeDriver();

【问题讨论】:

  • 简单猜测:尝试用 chromedriver.exe 的完整(绝对)路径替换 System.addProperty("webdriver.chrome.driver", ".../chromedriver.exe");

标签: c# .net webdriver selenium-webdriver


【解决方案1】:

由于您使用的是 C#,因此您应该使用 ChromeDriver 的构造函数重载,它允许您指定包含 chromedriver.exe 的目录的路径。也就是说:

IWebDriver driver = new ChromeDriver(@"C:\my\path\to\chromedriver\directory");

【讨论】:

  • 哇,我很确定我已经尝试过了,但显然没有。谢谢。
  • 感谢吉姆的回答。它刚刚对我有用。 +1!
  • 我为什么要突然使用这个构造函数?
  • @AndersLindén 这不是“突然”。这是 .NET 绑定从不支持从环境变量中读取 ChromeDriver 可执行文件的位置。许多人将环境变量的使用与 Java 绑定对System.addProperty 的使用混为一谈。它们不是一回事。
  • 但是我已经使用默认构造函数几年了?
【解决方案2】:

老问题,新答案(对于它的价值):只需安装 Nuget 包 Selenium.WebDriver.ChromeDriver。 Chromedriver.exe 将在下一次构建时位于 bin/debug 目录中。

第 3 方编辑 2017-09

在这个github page jsakamoto/nupkg-selenium-webdriver-chromedriver/ 上运行Install-Package Selenium.WebDriver -Version 3.5.2 后,chromedriver(.exe) 位于此文件夹下

" {解决方案文件夹} /packages/Selenium.WebDriver.ChromeDriver. {ver} /driver/ {平台}"

【讨论】:

  • 它确实为我将它复制到了 bin 但仍然抱怨它找不到它 - 虽然我正在使用 NCrunch 并且 NCrunch 可能从另一个目录运行;(
  • @ppumkin 你有没有想过这个问题?
  • 是的,我只是按照帖子中的建议提供了文件的完整路径。不知道为什么,但确实有效。所以我只是将完整路径放在我的应用程序配置中并使用路径对其进行初始化,因为我的每个环境都做不同的事情。
  • 这应该是答案
【解决方案3】:

这可能是因为 NuGet 包是从全局位置而不是 .NET Framework 项目的包文件夹加载的。这对我有用:

IWebDriver driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

【讨论】:

  • 这个简单的答案结合安装 NuGet 包 Selenium.Chrome.WebDriver 为我修复了它。
  • 这也是为我做的。
  • 我在 bin 文件夹中有 chromedriver.exe,但是找不到它,但是,在 Chrome 驱动程序服务上使用此答案中的路径有效: var driverService = ChromeDriverService.CreateDefaultService(Path .GetDirectoryName(Assembly.GetExecutingAssembly().Location));
【解决方案4】:

从 NuGet 安装 Selenium.WebDriver.ChromeDriver,然后您可以执行以下操作:

IWebDriver driver = new ChromeDriver(Environment.CurrentDirectory);

【讨论】:

  • 这似乎是解决问题的最简单方法。
  • 我已经在构造函数中使用了服务和选项,例如 IWebDriver driver = new ChromeDriver(service, options);我怎样才能添加 Environment.CurrentDirectory?
【解决方案5】:
you may have enum for your all drivers : 
  public enum Drivers
    {
        Chrome,
        Firefox,
        Safari,
        Edge,
        IE
    }


  public static IWebDriver GetDriver(Drivers driver)
        {

outPutDirectory -> 是在构建解决方案时复制所有支持的 dll 和文件的位置。 示例:C:\Users\Mike\source\repos\Automation\Automation\bin\Debug

     var outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
     // below is my location where I copied all drivers like chromedriver.exe 

relativePath -> 是您构建解决方案时正在复制的文件夹之一 示例:C:\Users\Mike\source\repos\Automation\Automation\bin\Debug\BrowserDriver

        var relativePath = @"..\..\bin\Debug\BrowserDriver"; 

//因此,无论您在哪台机器或 PC 上运行自动化,“chromeDriverPath”都会为您提供驱动程序的确切位置

       var chromeDriverPath = Path.GetFullPath(Path.Combine(outPutDirectory,relativePath));
    // return this driver , just debug this code and check the "outPutDirectory" path
       return new ChromeDriver(chromeDriverPath);
   }

【讨论】:

  • 对答案提供一些解释或简要说明。
  • 现在好了。请记住,当您发布答案时,不要只发布链接和代码。如果您在答案栏中仅提供代码和链接而没有解释,您可能会遭到反对。
【解决方案6】:

我发现虽然 Selenium.WebDriver.ChromeDriver NuGet 包已经下载,因此 chromedriver.exe 文件在编译时被复制到 bin 文件夹中,另外它需要标记为部署项(因为它是从 TestResults 文件夹复制到/运行的单元测试) - 即

[DeploymentItem(@"chromedriver.exe")]

【讨论】:

    【解决方案7】:

    这是一个具有挑战性的隔离 - clue is in the nuget source which contains Selenium.WebDriver.ChromeDriver.targets - 目标需要明确的属性分配,因此 chromedriver.exe 永远不会复制到 vstest.console 部署目录。这是添加到您的 CSPROJ 文件的修复:

    在 CSPROJ 中分配 PublishChromeDriver 属性

      <PropertyGroup>
        <AssemblyName>MyUX.Tests</AssemblyName>
         <!-- ... -->
        <PublishChromeDriver>True</PublishChromeDriver>
      </PropertyGroup>
    

    定义此属性后,chromedriver.exe 的副本将被复制到 /binvstest.console。这修复了我们收到的错误:

    chromedriver.exe file does not exist in the current directory or in a directory on the PATH environment variable. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html
    

    替代方法 - CSPROJ 中的强制复制

      <Target Name="CopyChromeDriverToBin" BeforeTargets="AfterBuild">
        <Copy SourceFiles="$(ChromeDriverSrcPath)" DestinationFiles="$(TargetDir)$(ChromeDriverName)" SkipUnchangedFiles="true">
        </Copy>
      </Target>
    

    【讨论】:

      【解决方案8】:

      这是我看到的错误: OpenQA.Selenium.DriverServiceNotFoundException:chromedriver.exe 文件不存在于当前目录或 PATH 环境变量的目录中。

      我通过在命令中指定“testsettings”参数来运行单元测试解决了这个问题。

      例如

      E:\Development\SampleProject\SampleProject.MvcWebApp\SampleProject.MvcWebApp.JavaScriptUnitTests\JavaScriptUnitTests\bin\Debug>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\mstest.exe" /testcontainer:JavaScriptUnitTests.dll /category:"JavaScriptUnitTests" /testsettings:..\..\..\Local.Testsettings /resultsfile:..\..\..\..\..\MsTestResults\SampleProject.MvcWebApp.JavaScript.Tests.trx
      

      我使用“/testsettings:......\Local.Testsettings”,因为 Local.testsettings 文件比我执行此命令的级别高 4 个级别。你应该相应地改变它。

      这是ccnet.config文件中使用的命令

      <exec>
          <executable>C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\mstest.exe</executable>
          <baseDirectory>SampleProject.MvcWebApp\SampleProject.MvcWebApp.JavaScriptUnitTests\JavaScriptUnitTests\bin\Debug</baseDirectory>
          <buildArgs>/testcontainer:JavaScriptUnitTests.dll /category:"JavaScriptUnitTests" /testsettings:..\..\..\Local.Testsettings /resultsfile:..\..\..\..\..\MsTestResults\SampleProject.MvcWebApp.JavaScript.Tests.trx</buildArgs>
          <successExitCodes>0</successExitCodes>
      </exec>
      

      【讨论】:

        【解决方案9】:

        如果您使用的是 Atata 和 .Net Core,请参阅此页面:https://atata.io/getting-started/#dot-net-core-configuration

         AtataContext.Configure()
                        .UseChrome()
                        .WithFixOfCommandExecutionDelay()
                        .WithLocalDriverPath()
                        .UseCulture("en-us")
                        .Build();
        

        这些是您要确保拥有的行:

        .UseChrome()
        .WithFixOfCommandExecutionDelay()
        .WithLocalDriverPath()
        

        【讨论】:

          【解决方案10】:

          我已经在我的 c# 控制台应用程序中安装了 nuget 包,构建后 bin/Debug 文件夹中没有“chromedriver.exe”。所以我手动下载了我的chrome版本的chromedriver,并手动将其复制到目录中,然后它就可以工作了。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-08-16
            • 1970-01-01
            • 2015-10-10
            • 2013-07-26
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多