Selenium WebDriver C#测试脚本中不存在ChromeDriver
本文关键字:不存在 ChromeDriver 测试脚本 WebDriver Selenium | 更新日期: 2023-09-27 18:21:59
在实例化驱动程序之前,我遇到过一些有同样问题的人,他们似乎已经解决了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");
老问题,新答案(就其价值而言):只需安装Nuget软件包Selenium.WebDriver.ChromeDriver.Chremediver.exe将在下一次构建时位于目录bin/debug中。
第三方编辑2017-09
在这个github页面上,jsakamoto/nupkg selenium webdriver chromedriver/运行Install-Package Selenium.WebDriver -Version 3.5.2
后,chromedriver(.exe)位于这个文件夹下面
"{解决方案文件夹}/packages/Selenium.WebDriver.ChromeDriver.{ver}/驱动程序/{平台}"
IWebDriver driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
从NuGet安装Selenium.WebDriver.ChromeDriver
,然后可以执行以下操作:
IWebDriver driver = new ChromeDriver(Environment.CurrentDirectory);
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上运行Automation ,"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);
}
我发现,尽管Selenium.WebDriver.ChromeDriver NuGet包已经下载,因此ChromeDriver.exe文件在编译时被复制到bin文件夹中,但它还需要标记为部署项(因为它是一个复制到TestResults文件夹中/从中运行的单元测试)-即
[DeploymentItem(@"chromedriver.exe")]
这是一个很难隔离的问题-线索在包含Selenium.WebDriver.ChromeDriver.targets
的nuget源中-目标需要显式的属性分配,因此chromedriver.exe
永远不会复制到vstest.console
部署目录中。以下是要添加到CSPROJ文件的修复程序:
在CSPROJ中分配PublishChromeDriver
属性
<PropertyGroup>
<AssemblyName>MyUX.Tests</AssemblyName>
<!-- ... -->
<PublishChromeDriver>True</PublishChromeDriver>
</PropertyGroup>
定义此属性后,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中的强制复制替代方法
<Target Name="CopyChromeDriverToBin" BeforeTargets="AfterBuild">
<Copy SourceFiles="$(ChromeDriverSrcPath)" DestinationFiles="$(TargetDir)$(ChromeDriverName)" SkipUnchangedFiles="true">
</Copy>
</Target>
我已经在c#控制台应用程序中安装了nuget包,在构建之后,bin/Debug文件夹中没有"chromedriver.exe"。所以我手动下载了chrome版本的chromedriver,并手动将其复制到目录中,然后它就工作了。
这是我看到的错误: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>
如果您使用的是Atata和.Net Core,请参阅此页:https://atata.io/getting-started/#dot-网芯配置
AtataContext.Configure()
.UseChrome()
.WithFixOfCommandExecutionDelay()
.WithLocalDriverPath()
.UseCulture("en-us")
.Build();
这些是你想确保你有:
.UseChrome()
.WithFixOfCommandExecutionDelay()
.WithLocalDriverPath()