繁体   English   中英

使用 Specflow 启动 Chrome 驱动程序时 - chromedriver.exe 文件不存在错误

[英]When Launching Chrome Driver using Specflow - chromedriver.exe file does not exist error

因此,我使用 Visual Studio 并安装了 Specflow 扩展、Specflow 项目,并且有一个类库项目,其中包含启动 Chrome 驱动程序的方法。 我用 Specflow 编写的测试是;

    LaunchDriver launchDriver = new LaunchDriver() ;

    [Given(@"I Launch a Chrome Driver")]
    public void GivenILaunchAChromeDriver()
    {
        launchDriver.LaunchChrome();
    }

LaunchDriver 类的代码是:

public class LaunchDriver
{
    public IWebDriver webDriver;

    public void LaunchChrome()
    {
        string PathtoChromeExe = @"C:\Program Files (x86)\Google\Chrome\Application\";
        webDriver = new ChromeDriver(PathtoChromeExe);
        webDriver.Manage().Timeouts().PageLoad = TimeSpan.FromMinutes(5);
        webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
        webDriver.Manage().Window.Maximize();
    }
}

当我尝试通过测试资源管理器运行此测试时,我看到由于以下错误导致测试失败

启动驱动程序并导航到 Google.com 持续时间:378 毫秒

  Message: 
    The file C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe does not exist. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html

  Stack Trace: 
    OpenQA.Selenium.DriverServiceNotFoundException: The file C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe does not exist. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html
    DriverService.ctor(String servicePath, Int32 port, String driverServiceExecutableName, Uri driverServiceDownloadUrl)
    ChromiumDriverService.ctor(String executablePath, String executableFileName, Int32 port, Uri downloadUrl)
    ChromeDriverService.ctor(String executablePath, String executableFileName, Int32 port)
    ChromeDriverService.CreateDefaultService(String driverPath, String driverExecutableFileName)
    ChromeDriverService.CreateDefaultService(String driverPath)

我需要启动的 Chrome 驱动程序,它的文件名是我指定的路径中的 chrome.exe,但由于某种原因,Specflow 或系统正在寻找 chromedriver.exe 来启动它找不到。 如何配置我的代码以启动 chrome.exe 而不是 chromedriver.exe?

关于“chromedriver”和“chrome”可能有些混淆。

文件chrome.exe是网络浏览器 Google Chrome 的可执行文件。 名为chromedriver.exe的文件会在您的计算机上启动一个服务器,该服务器充当您的自动化代码和 Google Chrome 之间的桥梁。

ChromeDriver(string)构造函数需要指向ChromeDriver.exe的路径, ChromeDriver.exe不是指向 Web 浏览器可执行文件的路径。

如果需要指定 Google Chrome 浏览器可执行文件的位置,则需要使用选项对象初始化 ChromeDriver:

public void LaunchChrome()
{
    var options = new ChromeDriverOptions()
    {
        BinaryLocation = @"C:\Program Files (x86)\Google\Chrome\Application\"
    };

    webDriver = new ChromeDriver(options);
    webDriver.Manage().Timeouts().PageLoad = TimeSpan.FromMinutes(5);
    webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
    webDriver.Manage().Window.Maximize();
}

此代码将在该特定位置使用 chrome.exe,但在 Windows %PATH% 中搜索 ChromeDriver.exe。 如果您还需要指定 ChromeDriver.exe 的路径,请将其作为字符串传递到第一个参数中:

var options = new ChromeDriverOptions()
{
    BinaryLocation = @"C:\Program Files (x86)\Google\Chrome\Application\"
};

var driverPath = @"C:\path\to\folder\containing\chromedriver";

webDriver = new ChromeDriver(driverPath, options);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM