Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium WebDriver C#测试脚本中不存在ChromeDriver_C#_.net_Webdriver_Selenium Webdriver - Fatal编程技术网

Selenium WebDriver C#测试脚本中不存在ChromeDriver

Selenium WebDriver C#测试脚本中不存在ChromeDriver,c#,.net,webdriver,selenium-webdriver,C#,.net,Webdriver,Selenium Webdriver,我遇到过一些人,他们的问题似乎与System.addProperty(“webdriver.chrome.driver”、“../chromedriver.exe”)的问题相同在实例化驱动程序之前 我在这方面运气不好,但仍然得到一个错误,即文件../bin/Debug/chromedriver.exe不存在 有没有人在没有把它放进垃圾箱文件夹的情况下运行了它 示例代码: System.Environment.SetEnvironmentVariable("webdriver.chrome.dri

我遇到过一些人,他们的问题似乎与
System.addProperty(“webdriver.chrome.driver”、“../chromedriver.exe”)的问题相同在实例化驱动程序之前

我在这方面运气不好,但仍然得到一个错误,即文件
../bin/Debug/chromedriver.exe
不存在

有没有人在没有把它放进垃圾箱文件夹的情况下运行了它

示例代码:

System.Environment.SetEnvironmentVariable("webdriver.chrome.driver", @"c:\path\to\driver\chromedriver.exe");
BrowserDriver = new ChromeDriver();
由于您使用的是C#,因此应该使用ChromeDriver的构造函数重载,该重载允许您指定包含ChromeDriver.exe的目录的路径。也就是说:

IWebDriver driver = new ChromeDriver(@"C:\my\path\to\chromedriver\directory");
这就是我看到的错误: OpenQA.Selenium.DriverServiceNotFoundException:chromedriver.exe文件不存在于当前目录或PATH环境变量的目录中。

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

例如

我使用“/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>

C:\ProgramFiles(x86)\Microsoft Visual Studio 10.0\Common7\IDE\mstest.exe
SampleProject.MvcWebApp\SampleProject.MvcWebApp.JavaScriptUnitTests\JavaScriptUnitTests\bin\Debug
/testcontainer:JavaScriptUnitTests.dll/category:“JavaScriptUnitTests”/testsettings:..\..\..\..\Local.testsettings/resultsfile:..\..\..\..\..\..\..\MsTestResults\SampleProject.MvcWebApp.JavaScript.Tests.trx
0
旧问题,新答案(不管它值多少):只需安装Nuget软件包Selenium.WebDriver.ChromeDriver。Chromedriver.exe将在下一次生成时位于bin/debug目录中

第三方编辑2017-09 在此基础上,运行
安装包Selenium.WebDriver-Version 3.5.2
后,chromedriver(.exe)位于此文件夹下方

“{solution folder}/packages/Selenium.WebDriver.ChromeDriver.{ver} /驱动程序/{platform}”


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

[DeploymentItem(@“chromedriver.exe”)]

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->是构建soltuion时复制的文件夹之一 示例:C:\Users\Mike\source\repos\Automation\Automation\bin\Debug\BrowserDriver

        var relativePath = @"..\..\bin\Debug\BrowserDriver"; 
//因此,“chromeDriverPath”将为您提供驱动程序的准确位置,无论您运行的是哪台机器或PC

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

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

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

这是一个具有挑战性的问题,要隔离目标需要显式的属性分配,因此
chromedriver.exe
永远不会复制到
vstest.console
部署目录。以下是要添加到CSPROJ文件的修复程序:

在CSPROJ中分配
PublishChromeDriver
属性

MyUX.测试
真的
定义此属性后,将
chromedriver.exe的副本复制到
vstest.console
/bin
。这修复了我们收到的错误:

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中强制复制
从NuGet安装Selenium.WebDriver.ChromeDriver,然后您可以执行以下操作:

IWebDriver driver = new ChromeDriver(Environment.CurrentDirectory);

如果您使用的是Atata和.Net Core,请参阅本页:

以下是您要确保的行:

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

我已经在我的c#控制台应用程序中安装了nuget软件包,在构建之后,bin/Debug文件夹中没有“chromedriver.exe”。因此,我手动下载了我的chrome版本的chromedriver,并将其手动复制到目录中,然后运行起来。

简单猜测:尝试替换
System.addProperty(“webdriver.chrome.driver”,“../chromedriver.exe”)
对于chromedriver.exewo的完整(绝对)路径,我很确定我已经尝试过了,但显然没有。谢谢。谢谢你的回答,吉姆。刚才对我很管用+1.为什么我要突然使用这个构造函数?@AndersLindén这不是“突然”。而是.NET绑定从未支持从环境变量读取ChromeDriver可执行文件的位置。许多人将环境变量的使用与Java绑定对
System.addProperty
的使用混为一谈。它们不是同一件事。但我已经使用默认构造函数好几年了?它确实为我将它复制到了bin,但仍然抱怨它找不到它-尽管我正在使用NCrunch,NCrunch可能是从另一个目录运行的;(@ppumkin)你知道吗?是的,我只是按照一篇帖子中的建议提供了文件的完整路径。不知道为什么,但是是的。所以我只是把完整路径放在我的应用程序配置中,并使用路径初始化它,因为我的每个环境都做了不同的事情。这应该是答案。提供一些解释或简要说明答案。现在没关系。记住,当你发布答案时,不要只发布链接和代码。当你只提供代码和链接而不在答案栏中进行解释时,你可能会获得否决票。这个简单的答案与安装NuGet软件包Selenium.Chrome.WebDriver相结合,为我解决了这个问题。这也为我解决了这个问题。我有chromedriver.exe位于bin文件夹中,但它无法找到它,但是,使用的是来自此答案的路径
IWebDriver driver = new ChromeDriver(Environment.CurrentDirectory);
 AtataContext.Configure()
                .UseChrome()
                .WithFixOfCommandExecutionDelay()
                .WithLocalDriverPath()
                .UseCulture("en-us")
                .Build();
.UseChrome()
.WithFixOfCommandExecutionDelay()
.WithLocalDriverPath()