问题
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.
I have had little luck with this and am still getting the error that the file .../bin/Debug/chromedriver.exe
does not exist.
Has anyone had any luck getting this to run without putting it in the bin folder?
Example code:
System.Environment.SetEnvironmentVariable("webdriver.chrome.driver", @"c:\path\to\driver\chromedriver.exe");
BrowserDriver = new ChromeDriver();
回答1:
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.
3rd party edit 2017-09
On this github page jsakamoto/nupkg-selenium-webdriver-chromedriver/ that after running Install-Package Selenium.WebDriver -Version 3.5.2
the chromedriver(.exe) lies below this folder
" {solution folder} /packages/Selenium.WebDriver.ChromeDriver. {ver} /driver/ {platform}"
回答2:
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:
IWebDriver driver = new ChromeDriver(@"C:\my\path\to\chromedriver\directory");
回答3:
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:
IWebDriver driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
回答4:
Install Selenium.WebDriver.ChromeDriver
from NuGet and then you can do the following:
IWebDriver driver = new ChromeDriver(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 -> 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
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
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
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:
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.
[DeploymentItem(@"chromedriver.exe")]
回答7:
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.exe
is never copied to vstest.console
deployment directory. Here is the fix to add to your CSPROJ file:
Assign PublishChromeDriver
Property in CSPROJ
<PropertyGroup>
<AssemblyName>MyUX.Tests</AssemblyName>
<!-- ... -->
<PublishChromeDriver>True</PublishChromeDriver>
</PropertyGroup>
After this property is defined, a copy of chromedriver.exe
will be copied to /bin
for vstest.console
. This fixes the error we were receiving:
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
<Target Name="CopyChromeDriverToBin" BeforeTargets="AfterBuild">
<Copy SourceFiles="$(ChromeDriverSrcPath)" DestinationFiles="$(TargetDir)$(ChromeDriverName)" SkipUnchangedFiles="true">
</Copy>
</Target>
回答8:
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.
I resolved this problem by specifying the 'testsettings' argument in the command to run the unit tests.
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.
This is the command used in ccnet.config file
<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:
If you're using Atata and .Net Core, see this page: 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()
回答10:
I've installed the nuget package in my c# console application and after build there was no 'chromedriver.exe' in bin/Debug folder. So I manually downloaded the chromedriver for my version of chrome and copied it to the directory manually and then it worked.
来源:https://stackoverflow.com/questions/9088367/chromedriver-does-not-exist-in-selenium-webdriver-c-sharp-test-script