48

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();
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Highstead
  • 2,291
  • 3
  • 26
  • 30
  • 3
    Plain guess: Try replacing the `System.addProperty("webdriver.chrome.driver", ".../chromedriver.exe");` with full (absolute) path to the chromedriver.exe – Pavel Janicek Feb 01 '12 at 15:46

10 Answers10

82

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");
JimEvans
  • 27,201
  • 7
  • 83
  • 108
  • 1
    Wow, I was pretty sure i had tried that, but apparently not. Thanks. – Highstead Feb 02 '12 at 22:58
  • Why should I, all of a sudden use this constructor? – Anders Lindén Jun 07 '16 at 14:23
  • @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. – JimEvans Jun 07 '16 at 15:21
  • But I have used the default constructor for some years now? – Anders Lindén Jun 08 '16 at 05:58
  • @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`. – JimEvans Jun 08 '16 at 10:42
  • But it has automatically been copied into project\bin\Debug since I started to use it. Never in the path. I have used the default constructor without problems! – Anders Lindén Jun 08 '16 at 10:54
  • Sorry, my previous comment was imprecise. The .NET bindings look first in the same directory as the `WebDriver.dll` assembly, then in the path, if the specific path was not specified in the constructor. As long as the executable exists in one of those places, the .NET bindings will use it with the default constructor. This particular question was asking how to specify the location of the executable, and this answer addresses that. – JimEvans Jun 08 '16 at 15:16
  • +1: I had used the full path to the exe, instead of only the folder containing the exe. (The error I got probably told me this, but I missed it somehow.) I.e. should be `C:\my\path\to\chromedriver\directory` not `C:\my\path\to\chromedriver\directory\chromedriver.exe`. – Scott Weldon Jun 20 '16 at 21:21
  • I had to use the full path as well. There must be a way to use a relative path, however, I haven't figured that out yet. Perhaps using the project's bin folder will work. – bob.mazzo Sep 08 '16 at 17:09
49

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

surfmuggle
  • 5,527
  • 7
  • 48
  • 77
Mcanic
  • 1,304
  • 16
  • 22
  • 1
    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 ;( – Piotr Kula Jan 16 '17 at 18:58
  • @ppumkin did you ever figure this out? – Victorio Berra Aug 29 '17 at 20:52
  • 1
    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. – Piotr Kula Sep 04 '17 at 12:43
  • This should be the answer – Nebula Nov 03 '17 at 08:05
25

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));
Harun Davood
  • 251
  • 3
  • 3
  • 3
    This simple answer combined with installing the NuGet package Selenium.Chrome.WebDriver fixed it for me. – Neo May 27 '18 at 18:34
  • This did it for me as well. – puerile Nov 04 '20 at 13:51
  • 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)); – Fordy Mar 04 '21 at 10:29
17

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

IWebDriver driver = new ChromeDriver(Environment.CurrentDirectory);
mr.coffee
  • 962
  • 8
  • 22
  • This seems like the easiest solution to the problem. – Ola Eldøy Sep 10 '19 at 07:40
  • 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? – SoDamnMetal Jan 04 '21 at 09:23
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);
   }
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
Mike ASP
  • 2,013
  • 2
  • 17
  • 24
  • provide some explanation or a brief note to the answer. – HarshitMadhav Dec 21 '17 at 20:09
  • 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. – HarshitMadhav Dec 26 '17 at 18:12
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")]

TerrorBight
  • 334
  • 4
  • 23
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>
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
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.

Atiq Baqi
  • 612
  • 1
  • 7
  • 16
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>
Ali Lane
  • 95
  • 6
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()
Rafe
  • 8,467
  • 8
  • 47
  • 67