Selenium WebDriver C# 测试脚本中不存在 ChromeDriver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9088367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ChromeDriver does not exist in Selenium WebDriver C# test script
提问by Highstead
I have come across a few people with the same issue that seemed to have solved the problem with System.addProperty("webdriver.chrome.driver", ".../chromedriver.exe");before instantiating the driver.
我遇到过一些有同样问题的人,他们似乎System.addProperty("webdriver.chrome.driver", ".../chromedriver.exe");在实例化驱动程序之前解决了这个问题。
I have had little luck with this and am still getting the error that the file .../bin/Debug/chromedriver.exedoes not exist.
我在这方面运气不佳,并且仍然收到文件.../bin/Debug/chromedriver.exe不存在的错误。
Has anyone had any luck getting this to run without putting it in the bin folder?
有没有人有幸让它在不放入 bin 文件夹的情况下运行?
Example code:
示例代码:
System.Environment.SetEnvironmentVariable("webdriver.chrome.driver", @"c:\path\to\driver\chromedriver.exe");
BrowserDriver = new ChromeDriver();
采纳答案by Mcanic
Old question, new answer (for what it's worth): just install the Nuget package Selenium.WebDriver.ChromeDriver. Chromedriver.exe will be in the directory bin/debug on the next build.
老问题,新答案(就其价值而言):只需安装 Nuget 包 Selenium.WebDriver.ChromeDriver。Chromedriver.exe 将在下一次构建时位于目录 bin/debug 中。
3rd party edit 2017-09
第三者编辑 2017-09
On this github page jsakamoto/nupkg-selenium-webdriver-chromedriver/that after running Install-Package Selenium.WebDriver -Version 3.5.2the chromedriver(.exe)lies below this folder
在此GitHub的页面jsakamoto / nupkg -硒的webdriver-chromedriver /跑跑后Install-Package Selenium.WebDriver -Version 3.5.2的chromedriver文件(.exe)位于此文件夹下
" {solution folder} /packages/Selenium.WebDriver.ChromeDriver. {ver} /driver/ {platform}"
“ {解决方案文件夹} /packages/Selenium.WebDriver.ChromeDriver. {ver} /driver/ {platform}”
回答by JimEvans
Since you're using C#, you should use the constructor overload for ChromeDriver that allows you to specify the path to the directory containing chromedriver.exe. To wit:
由于您使用的是 C#,您应该使用 ChromeDriver 的构造函数重载,它允许您指定包含 chromedriver.exe 的目录的路径。以机智:
IWebDriver driver = new ChromeDriver(@"C:\my\path\to\chromedriver\directory");
回答by Ali Lane
This is the error i see: OpenQA.Selenium.DriverServiceNotFoundException: The chromedriver.exe file does not exist in the current directory or in a directory on the PATH environment variable.
这是我看到的错误: OpenQA.Selenium.DriverServiceNotFoundException:当前目录或 PATH 环境变量上的目录中不存在 chromedriver.exe 文件。
I resolved this problem by specifying the 'testsettings' argument in the command to run the unit tests.
我通过在运行单元测试的命令中指定“testsettings”参数解决了这个问题。
E.g.
例如
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
I use "/testsettings:......\Local.Testsettings" because the Local.testsettings file is 4 levels higher than the level where I am executing this command. You should change it accordingly.
我使用 "/testsettings:......\Local.Testsettings" 因为 Local.testsettings 文件比我执行此命令的级别高 4 个级别。你应该相应地改变它。
This is the command used in ccnet.config file
这是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>
回答by TerrorBight
I found that although the Selenium.WebDriver.ChromeDriver NuGet package had been downloaded and consequently the chromedriver.exe file was being copied into the bin folder at compile time, additionally it needed to be marked as a deployment item (because it is a unit test that copied-into/run-from the TestResults folder) - i.e.
我发现虽然已经下载了 Selenium.WebDriver.ChromeDriver NuGet 包,因此编译时将 chromedriver.exe 文件复制到 bin 文件夹中,但还需要将其标记为部署项(因为它是一个单元测试)复制到/从 TestResults 文件夹运行) - 即
[DeploymentItem(@"chromedriver.exe")]
[部署项(@"chromedriver.exe")]
回答by Mike ASP
you may have enum for your all drivers :
public enum Drivers
{
Chrome,
Firefox,
Safari,
Edge,
IE
}
public static IWebDriver GetDriver(Drivers driver)
{
outPutDirectory -> is a location where all supporting dlls and files are copied when you build the solution. example : C:\Users\Mike\source\repos\Automation\Automation\bin\Debug
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 -> is a one of folder being copied when you build soltuion exampple : C:\Users\Mike\source\repos\Automation\Automation\bin\Debug\BrowserDriver
relativePath -> 是构建解决方案示例时复制的文件夹之一:C:\Users\Mike\source\repos\Automation\Automation\bin\Debug\BrowserDriver
var relativePath = @"..\..\bin\Debug\BrowserDriver";
//So 'chromeDriverPath' will give you exact location of your driver no matter which machine or PC you are running Automation
//因此,无论您运行的是哪台机器或 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);
}
回答by Harun Davood
Could this be because NuGet packages are being loaded from a global place instead of the packages folder of the .NET Framework projects. This worked for me:
这可能是因为 NuGet 包是从全局位置而不是 .NET Framework 项目的包文件夹加载的。这对我有用:
IWebDriver driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
回答by SliverNinja - MSFT
This was a challenging one to isolate - the clue is in the nuget source which contains Selenium.WebDriver.ChromeDriver.targets- the targets requires an explicit property assignment so chromedriver.exeis never copied to vstest.consoledeployment directory. Here is the fix to add to your CSPROJ file:
这是一个具有挑战性的隔离 -线索在包含的 nuget 源中Selenium.WebDriver.ChromeDriver.targets- 目标需要明确的属性分配,因此chromedriver.exe永远不会复制到vstest.console部署目录。这是添加到您的 CSPROJ 文件的修复程序:
Assign PublishChromeDriverProperty in CSPROJ
PublishChromeDriver在 CSPROJ 中分配属性
<PropertyGroup>
<AssemblyName>MyUX.Tests</AssemblyName>
<!-- ... -->
<PublishChromeDriver>True</PublishChromeDriver>
</PropertyGroup>
After this property is defined, a copy of chromedriver.exewill be copied to /binfor vstest.console. This fixes the error we were receiving:
定义此属性后, 的副本chromedriver.exe将复制到/binfor vstest.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
Alternative Approach - Force Copy in CSPROJ
替代方法 - CSPROJ 中的强制复制
<Target Name="CopyChromeDriverToBin" BeforeTargets="AfterBuild">
<Copy SourceFiles="$(ChromeDriverSrcPath)" DestinationFiles="$(TargetDir)$(ChromeDriverName)" SkipUnchangedFiles="true">
</Copy>
</Target>
回答by mr.coffee
Install Selenium.WebDriver.ChromeDriverfrom NuGet and then you can do the following:
Selenium.WebDriver.ChromeDriver从 NuGet安装,然后您可以执行以下操作:
IWebDriver driver = new ChromeDriver(Environment.CurrentDirectory);
回答by Rafe
If you're using Atata and .Net Core, see this page: https://atata.io/getting-started/#dot-net-core-configuration
如果您使用 Atata 和 .Net Core,请参阅此页面:https://atata.io/getting-started/#dot-net-core-configuration
AtataContext.Configure()
.UseChrome()
.WithFixOfCommandExecutionDelay()
.WithLocalDriverPath()
.UseCulture("en-us")
.Build();
these are the lines you want to make sure you have:
这些是您要确保拥有的行:
.UseChrome()
.WithFixOfCommandExecutionDelay()
.WithLocalDriverPath()

