C# Selenium WebDriver.ChromeDriver Nuget 包已安装,但不适用于 MSTest

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17695742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:13:12  来源:igfitidea点击:

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

c#visual-studioselenium-webdrivermstestselenium-chromedriver

提问by Dave B 84

I have added the WebDriver.ChromeDrivernuget 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:

我已将WebDriver.ChromeDrivernuget 包添加到我的解决方案中,其中包含 Selenium WebDriver 使用 Chrome 运行自动化测试所需的 ChromeDriver.exe 文件。查看包内容,它只包含以下文件:

tools\chromedriver.exe

tools\chromedriver.exe

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

应该做的是将此文件夹添加到 PATH 环境变量,以便chromedriver.exe可以通过以下代码访问(这是在使用 MSTest 的 UnitTest 项目中):

[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.

chromedriver.exe 文件不存在于当前目录或 PATH 环境变量上的目录中。可以从http://code.google.com/p/chromium/downloads/list下载驱动程序。

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

查看 nuget 文档,它表明nuget 包的工具文件夹中的任何内容都将自动添加到 PATH 环境变量中:

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.

http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package:包的工具文件夹用于可从包管理器控制台访问的 powershell 脚本和程序。将文件夹复制到目标项目后,将其添加到`$env:Path (PATH) 环境变量中。

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.

但是,这似乎不起作用。我什echo %PATH%至在命令行上运行,但它没有将我的工具文件夹显示为已注册。

What am I doing wrong?

我究竟做错了什么?

采纳答案by Arran

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.

我不会为此烦恼 NuGet 包,仅仅因为这不是一个类库 - 从技术上讲,这就是 NuGet 的全部内容。自该版本发布以来,ChromeDriver 也进行了多次更新。

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.
  1. 下载 NuGet 包
  2. 使用 Visual Studio,通过右键单击项目 -> 添加项目 -> 现有项目向项目添加新项目
  3. 导航并选择 chromedriver.exe
  4. 更改属性以确保“复制到输出目录”设置为始终复制

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

您可能在第 4 点上失败了。该设置默认设置为不复制

回答by Raj Asapu

I set it to "Copy", which always which resulted in error when I try to rebuild it. Setting it to "Copy if newer" works fine.

我将它设置为“复制”,这在我尝试重建它时总是会导致错误。将其设置为“如果更新则复制”工作正常。

回答by Frankenstine Joe

I had similar problem solved it by these 3 steps

我通过这 3 个步骤解决了类似的问题

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

1.转到谷歌浏览器驱动官网 https://sites.google.com/a/chromium.org/chromedriver/downloads下载并解压

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

2.转到Visual Studio解决方案资源管理器单击添加->现有文件->选择chrome驱动程序

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

3.在VS中右击chrome驱动,选择属性设置为始终复制

回答by Kurkula

IWebDriver driver = new ChromeDriver("C:\Folder_with_Chrome_driver");

Download related driver and add local path.

下载相关驱动并添加本地路径。

回答by RnDrx

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.

Nuget 包会将 driver.exe 文件放置在 {buildconfiguration}/ 要告诉它在应用程序的根目录中查找,请传递一个“.”。创建驱动程序的新实例时。

IWebDriver driver = new ChromeDriver(".");

回答by mberger

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:

我不得不使用提供的答案的组合。首先,我必须在构建步骤中将 chromedriver.exe 复制到输出。我使用 Arrans 答案来做到这一点。如果您不想安装 VS,我怀疑您只需要更改您的项目文件以包含以下内容:

<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

它仍然没有奏效。然后我不得不通过将我的 chrome 驱动程序更改为使用 RnDrxs 答案来更改我的代码

new ChromeDriver(".");

回答by Anilkumar KH

its resolves the issue :

它解决了这个问题:

IWebDriver driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));