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

9
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个回答

9
在创建对象之前,请在您的代码中添加这些行。
   System.setProperty("webdriver.ie.driver", 
        "E:\\path where your IEDriverServer is located\\IEDriverServer.exe");

你可以从这里下载IEDriverServer.exe文件。

如果你正在使用C#,你可以使用下面的代码。

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

7

根据异常信息,你需要下载IEDriverServer,根据你所使用的IE版本选择32位或64位,并确保它在我们的路径中可用。当你在命令行中键入 IEDriverServer.exe 时,应该能够正常使用。请尝试这样做。


将尝试在命令提示符上执行并告知您结果。提前致谢。 - Amar HR

4

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

这篇文章包含下载链接和有关将其作为项目一部分的其他信息。


1
我已经下载了IEDriverServer并解压缩了它。 IEDriverServer.exe的路径已添加到PATH环境变量中。即使完成了所有这些先决条件,事情仍然没有按预期工作。 - Amar HR

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

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

因此,在.NET中将IEDriverServer放入%PATH%中是无效的。请使用非官方NuGet版本,其中包含IE驱动程序(它被放置在Packages目录中,并从测试项目引用),或者将其与项目捆绑在一起,并在首选项下标记exe为“如果较新则复制”。然后将相对路径添加到InternetExplorerDriver的构造函数中。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接