49

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
  • 3
    Plain guess: Try replacing the System.addProperty("webdriver.chrome.driver", ".../chromedriver.exe"); with full (absolute) path to the chromedriver.exe Commented Feb 1, 2012 at 15:46

10 Answers 10

84

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");
Sign up to request clarification or add additional context in comments.

9 Comments

Wow, I was pretty sure i had tried that, but apparently not. Thanks.
Why should I, all of a sudden use this constructor?
@AndersLindén It's not "all of a sudden." It's that the .NET bindings have never supported reading the location of the ChromeDriver executable from an environment variable. Many people conflate the use of an environment variable with the Java bindings' use of System.addProperty. They aren't the same thing.
But I have used the default constructor for some years now?
@AndersLindén If the driver executable is in one of the directories on your PATH environment variable, the default constructor should work fine. This solution is only required if you must specify the exact location of chromedriver.exe.
|
50

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}"

4 Comments

It did copy it to bin for me but still moaned it could not find it - though I am using NCrunch and NCrunch may be running from another directory ;(
@ppumkin did you ever figure this out?
Yea I just provided the FULL path the file as suggested in one the posts. Not sure why but yea that works. So I just put the full path in my application config and initialized it using the path, as each of my environments does different things.
This should be the answer
27

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));

3 Comments

This simple answer combined with installing the NuGet package Selenium.Chrome.WebDriver fixed it for me.
This did it for me as well.
I had the chromedriver.exe in the bin folder, but it couldn't find it, however, using the path from this answer on the Chrome Driver Service worked: var driverService = ChromeDriverService.CreateDefaultService(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
19

Install Selenium.WebDriver.ChromeDriver from NuGet and then you can do the following:

IWebDriver driver = new ChromeDriver(Environment.CurrentDirectory);

2 Comments

This seems like the easiest solution to the problem.
I'm already using service and options in the constructor like this IWebDriver driver = new ChromeDriver(service, options); How can I also add Environment.CurrentDirectory?
7
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);
   }

2 Comments

provide some explanation or a brief note to the answer.
Now it is okay. Remember when you post an answer don't post just links and codes only. You might get downvotes when you provide just code and links only without explanation in the answer column.
3

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")]

Comments

2

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>

Comments

2

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.

Comments

1

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>

Comments

1

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()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.