如何使用 C# 使用 Selenium WebDriver 实例化 InternetExplorerDriver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15247095/
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
How to instantiate InternetExplorerDriver with Selenium WebDriver using C#
提问by Amar HR
new InternetExplorerDriver();
But I could see exception as below:
但我可以看到如下异常:
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()
采纳答案by abhinav
As the exception says, you need to download IEDriverServer either 32 or 64 bit depending on IE you have and make sure it is available in our path. That is when you type IEDriverServer.exe on command line it should be resolved. Try that
正如例外所说,您需要根据您拥有的 IE 下载 32 位或 64 位的 IEDriverServer,并确保它在我们的路径中可用。那就是当您在命令行上键入 IEDriverServer.exe 时,它应该被解决。试试看
回答by Hemanth
Add these lines to your code before creating the object.
在创建对象之前将这些行添加到您的代码中。
System.setProperty("webdriver.ie.driver",
"E:\path where your IEDriverServer is located\IEDriverServer.exe");
You can download IEDriverServer.exe file from here.
您可以从这里下载 IEDriverServer.exe 文件。
As you are using C# you can use the below code.
当您使用 C# 时,您可以使用以下代码。
private const string IE_DRIVER_PATH = @"C:\PathTo\IEDriverServer";
var driver = new InternetExplorerDriver(IE_DRIVER_PATH, options);
回答by Peter Bernier
回答by cederlof
The .NET bindings don't scan the %PATH% environment variable for the executable.
.NET 绑定不会扫描可执行文件的 %PATH% 环境变量。
https://groups.google.com/forum/?fromgroups#!topic/webdriver/EvTyEPYchxE
https://groups.google.com/forum/?fromgroups#!topic/webdriver/EvTyEPYchxE
Hence, it does notwork to put IEDriverServer in the %PATH% for .NET.
因此,它并没有工作,把IEDriverServer在%PATH%的.NET。
Use the unofficial NuGet version with the IE-driver bundled (it is put in the Packages-dir and referenced from the test-project), or bundle it yourself with the project, and mark the exe as Copy if newerunder preferences. Then add the relative path into the constructor of InternetExplorerDriver
.
使用捆绑了 IE 驱动程序的非官方 NuGet 版本(它放在 Packages-dir 中并从测试项目中引用),或者将它自己与项目捆绑在一起,并在首选项下将 exe 标记为Copy if newer。然后将相对路径添加到InternetExplorerDriver
.
回答by ono2012
You can pass in the path to the IEDriverServer in an overload of the constructor
您可以在构造函数的重载中传递到 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);
so
所以
new InternetExplorerDriver("..\.."); // if it was two folders up