Selenium WebDriver.ChromeDriver Nuget package installed, but not working for MSTest

43,997

Solution 1

I wouldn't bother with the NuGet package for this, simply because this is not a class library - which is technically, what NuGet is all about. ChromeDriver has also been updated many times since that release.

Anyway, I would say that I have just done the same thing to see what the issue is:

  1. Download NuGet package
  2. Using Visual Studio, add a new item to the project by right-clicking on the project -> Add Item -> Existing Item
  3. Navigate and select the chromedriver.exe
  4. Change the properties to ensure "Copy to Output Directory" is set to Copy always.

You are probably falling down on point 4. That setting is set to Do not copy by default.

Solution 2

The Nuget package will place the driver.exe file in {buildconfiguration}/ To tell it to look in the root of the application, pass a "." when creating a new instance of the driver.

IWebDriver driver = new ChromeDriver(".");

Solution 3

I had to use a combination of the answers provided. First, I had to make the build step copy the chromedriver.exe to the output. I used Arrans answer to do this. If you don't want to install VS, I suspect that to you only need to change your project file to include this:

<ItemGroup>
    <None Update="chromedriver.exe">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

It still didn't work. I then had to change my code to use RnDrxs answer by changing my chrome driver to

new ChromeDriver(".");

Solution 4

I had similar problem solved it by these 3 steps

1.Goto google chrome drivers official site https://sites.google.com/a/chromium.org/chromedriver/downloads download and unpack

2.Goto Visual Studio solution explorer click add -> existing file -> select chrome driver

3.Right click on chrome driver in VS and select properties set it to always copy

Share:
43,997
Dave B 84
Author by

Dave B 84

Updated on October 18, 2020

Comments

  • Dave B 84
    Dave B 84 over 3 years

    I have added the WebDriver.ChromeDriver nuget package to my solution, which contains the ChromeDriver.exe file, required for Selenium WebDriver to run automated tests using Chrome. Looking at the package contents, it just contains the file following file:

    tools\chromedriver.exe

    What this is supposed to do is add this folder to the PATH environment variable so that chromedriver.exe is accessible via the following code (this is in a UnitTest project using MSTest):

    [TestMethod]
    public void LaunchWebsite_Chrome()
    {
         // create ChromeDriver - this should work if chromedriver.exe 
         // is known to the environment PATH variable
         IWebDriver driver = new ChromeDriver();
    
         driver.Navigate().GoToUrl("http://localhost/");
    }
    

    However, I am still getting the following exception:

    The 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://code.google.com/p/chromium/downloads/list.

    Looking at the nuget documentation, it suggests that anything in the tools folder of the nuget package will get added to the PATH environment variable automatically:

    http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package: The tools folder of a package is for powershell scripts and programs accessible from the Package Manager Console. After the folder is copied to the target project, it is added to the `$env:Path (PATH) environment variable.

    However, this doesn't seem to be working. I have even run echo %PATH% on the command line and it doesn't show my tools folder as registered.

    What am I doing wrong?

  • Dave B 84
    Dave B 84 almost 11 years
    Adding the file manually is a good work around, but any ideas why NuGet is not adding the "tools" folder to the PATH environment variable? (It's documentation suggests that it should)
  • Matthew Lock
    Matthew Lock over 8 years
    I wonder if Visual Studio has to be run as administrator for the path variables to be able to be set?
  • Frank H.
    Frank H. about 7 years
    I tried this - adding the diver manually in the project root, after installing the NuGet package. The result was that every time I tried to run the code, TWO instances of chromedriver.exe was started. And then when I tried to run it again, it didn't work because the chromedriver.exe was in use by another process. Switching from Release to Debug worked exactly ONCE, then never again. This solution is at best flaky, and should NOT have been accepted as an answer imo.
  • Arran
    Arran about 7 years
    @FrankH. probably, but I never experienced that issue, so it's possible you've got something else going on. This is an old answer so it's entirely possible there are better ways of achieving it.
  • Swimburger
    Swimburger almost 5 years
    I wouldn't bother downloading the unverified Chromedriver Nuget packages and grab it directly from sites.google.com/a/chromium.org/chromedriver/downloads to be safe.
  • Swimburger
    Swimburger almost 5 years
    This would be safer than grabbing the Chromedriver from unverified Nuget packages. Thanks for the tip.
  • lotor
    lotor over 4 years
    explain you advise
  • Daniel Williams
    Daniel Williams over 4 years
    the reason for the nuget package is that you can keep up with the most recent version of the driver. If you just have that one and only one chromedriver.exe, it will be out of data in a few months and your code will fail. So how to keep it updated reliably?
  • Arran
    Arran over 4 years
    You aren't keeping up to date with using said package because you are dependent on the author keeping it up to date. Besides you do not want to have things magically update...you should be pinning your dependencies to specific versions known to work with your code and the rest of the dependency tree.
  • Martin
    Martin over 4 years
    Yes, explain it!
  • Sarang M K
    Sarang M K over 2 years
    Assembly.GetExecutingAssembly().Location returns the current location from where your code is currently getting executed.