如何使用C#使用Selenium WebDriver实例化InternetExplorerDriver

时间:2013-03-06 12:14:04

标签: c# selenium selenium-webdriver remotewebdriver iedriverserver

new InternetExplorerDriver();

但我可以看到例外如下:

OpenQA.Selenium.DriverServiceNotFoundException was unhandled by user code
  HResult=-2146233088
  Message=The IEDriverServer.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/selenium/downloads/list.
  Source=WebDriver
  StackTrace:
       at OpenQA.Selenium.DriverService.FindDriverServiceExecutable(String executableName, Uri downloadUrl)
       at OpenQA.Selenium.IE.InternetExplorerDriverService.CreateDefaultService()
       at OpenQA.Selenium.IE.InternetExplorerDriver..ctor(InternetExplorerOptions options)
       at OpenQA.Selenium.IE.InternetExplorerDriver..ctor()
       at Accelrys.CommonTestFramework.WebActions.WebActionLibrary.CreateSeleniumDriver()

5 个答案:

答案 0 :(得分:9)

在创建对象之前,将这些行添加到代码中。

   System.setProperty("webdriver.ie.driver", 
        "E:\\path where your IEDriverServer is located\\IEDriverServer.exe");

您可以从here下载IEDriverServer.exe文件。

当您使用C#时,您可以使用以下代码。

private const string IE_DRIVER_PATH = @"C:\PathTo\IEDriverServer";
var driver = new InternetExplorerDriver(IE_DRIVER_PATH, options);

答案 1 :(得分:7)

如异常所述,您需要根据您拥有的IE下载32位或64位IEDriverServer,并确保它在我们的路径中可用。也就是说,当您在命令行上键入IEDriverServer.exe时,它应该被解析。试试那个

答案 2 :(得分:4)

您需要安装IEDriverServer并将其作为项目的一部分。

This Post包含下载链接以及一些有关使其成为项目一部分的其他信息。

答案 3 :(得分:2)

  

.NET绑定不会扫描%PATH%环境变量   可执行文件。

https://groups.google.com/forum/?fromgroups#!topic/webdriver/EvTyEPYchxE

因此,将IEDriverServer放入.NET的%PATH%中。

将非官方NuGet版本与捆绑的IE驱动程序一起使用(将其放在Packages-dir中并从test-project中引用),或者将其自行捆绑到项目中,并将exe标记为 Copy if较新的在首选项下。然后将相对路径添加到InternetExplorerDriver

的构造函数中

答案 4 :(得分:0)

您可以在构造函数

的重载中传递路径到IEDriverServer
namespace OpenQA.Selenium.IE
    //
    // Summary:
    //     Initializes a new instance of the OpenQA.Selenium.IE.InternetExplorerDriver class
    //     using the specified path to the directory containing IEDriverServer.exe.
    //
    // Parameters:
    //   internetExplorerDriverServerDirectory:
    //     The full path to the directory containing IEDriverServer.exe.
    public InternetExplorerDriver(string internetExplorerDriverServerDirectory);

所以

new InternetExplorerDriver("..\.."); // if it was two folders up