如何设置硒3.0,出现错误"geckodriver.exe文件不存在..."在C#中 [英] How to set up selenium 3.0, getting error "The geckodriver.exe file does not exist..." in c#
问题描述
将Visual Studio中的Selenium更新为3.0,将Firefox更新为47.0,现在当我尝试使用本地WebDriver模式时出现此错误: geckodriver.exe文件在当前目录或PATH环境变量上的目录中不存在.
Updated selenium in visual studio to 3.0 and firefox to 47.0 and now I'm getting this error when I try to use local webdriver mode: The geckodriver.exe file does not exist in the current directory or in a directory on the PATH environment variable.
当我使用远程模式(seleniumhub)时,即使使用的是firefox 45.0版本,它也可以正常工作.
When I'm using remote mode (seleniumhub), it works fine even if it uses firefox 45.0 version.
试图搜索一些示例,但没有找到任何针对c#的内容,仅针对Java,并且仍然无法使其正常工作.
Tried to search for some examples, but did not found anything for c#, only for java and still could not make it work.
我的网络驱动程序设置:
my webdriver setup:
switch (ConfigurationManager.AppSettings["WebDriverMode"].ToLower())
{
case "local":
switch (ConfigurationManager.AppSettings["WebDriverBrowserCapabilities"].ToLower())
{
case "firefox":
driver = new AdvancedFirefoxDriver();
break;
case "ie":
driver = new AdvancedInternetExplorerDriver();
break;
case "chrome":
driver = new AdvancedChromeDriver();
break;
default:
throw new NotImplementedException(string.Format("WebDriverBrowserCapabilities of \"{0}\" is not implemented for {1} mode", ConfigurationManager.AppSettings["WebDriverBrowserCapabilities"].ToLower(), ConfigurationManager.AppSettings["WebDriverMode"].ToLower()));
}
break;
case "remote":
var huburl = new Uri(ConfigurationManager.AppSettings["SeleniumHubAddress"]);
DesiredCapabilities capabilities;
switch (ConfigurationManager.AppSettings["WebDriverBrowserCapabilities"].ToLower())
{
case "firefox":
capabilities = DesiredCapabilities.Firefox();
break;
case "ie":
capabilities = DesiredCapabilities.InternetExplorer();
break;
case "chrome":
capabilities = DesiredCapabilities.Chrome();
break;
default:
throw new NotImplementedException(string.Format("WebDriverBrowserCapabilities of \"{0}\" is not implemented for {1} mode", ConfigurationManager.AppSettings["WebDriverBrowserCapabilities"].ToLower(), ConfigurationManager.AppSettings["WebDriverMode"].ToLower()));
}
capabilities.IsJavaScriptEnabled = true;
driver = new AdvancedRemoteWebDriver(huburl, capabilities);
break;
default:
throw new NotImplementedException();
}
推荐答案
在selenium 3.0中,必须将geckodriver
用于Firefox浏览器.
From selenium 3.0, you have to use the geckodriver
for Firefox browser.
从此处 https://github.com/mozilla/geckodriver/releases
您有两个选择:
- 在Windows系统环境变量
PATH
中输入geckodriver路径. - 或以编程方式指定geckodriver.exe的位置,如下所示.
- enter geckodriver path in Windows System Environment Variable
PATH
. - Or specify the location of the geckodriver.exe programmatically as follows.
System.Environment.SetEnvironmentVariable("webdriver.gecko.driver",@"/path/to/geckodriver.exe"
注意:如果您设置PATH环境变量,则可能需要重新启动系统.
Note: System restart may be required if you set PATH environment variable.
从Firefox 47开始(不包括Firefox),Selenium默认使用geckodriver功能.对于47及以后的版本,您可能需要关闭此功能,以便Selenium可以像以前使用这些版本一样使用Firefox内置支持.
From Firefox 47 onwards (Excluding it), Selenium uses geckodriver capabilities by default. For 47 and previous versions onwards, you may need to turn off this capability so that Selenium can use Firefox built-in support like we used to work with these versions.
实现相同的JAVA版本:
JAVA version to achieve the same:
DesiredCapabilities d = new DesiredCapabilities();
d.setCapability("marionette", false); // to disable marionette.
WebDriver driver = new FirefoxDriver(d);
参考文献:
- 如何在C#中设置系统属性
- https://msdn.microsoft.com/en-us/library/z46c489x.aspx
- https://superuser.com/questions/317631/setting-path -in-windows-7-command-prompt
- https://stackoverflow.com/a/40466109/2575259
- how to set system properties in C#
- https://msdn.microsoft.com/en-us/library/z46c489x.aspx
- https://superuser.com/questions/317631/setting-path-in-windows-7-command-prompt
- https://stackoverflow.com/a/40466109/2575259
这篇关于如何设置硒3.0,出现错误"geckodriver.exe文件不存在..."在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!