如何设置selenium 3.0,在c#中收到错误"geckodriver.exe文件不存在..."

saf*_*ary 8 c# selenium webdriver selenium-webdriver selenium3

将visual studio中的selenium更新为3.0,firefox更新为47.0,现在我尝试使用本地webdriver模式时出现此错误:geckodriver.exe文件不存在于当前目录或PATH环境变量的目录中.

当我使用远程模式(seleniumhub)时,即使它使用firefox 45.0版本也能正常工作.

试图搜索一些例子,但没有为c#找到任何东西,只为java而且仍然无法使它工作.

我的webdriver设置:

 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();
                }
Run Code Online (Sandbox Code Playgroud)

Nav*_*R B 8

从selenium 3.0开始,您必须使用geckodriverfor Firefox浏览器.

从这里下载最新的geckodriver https://github.com/mozilla/geckodriver/releases

您有两种选择:

  1. 在Windows系统环境变量中输入geckodriver路径PATH.
  2. 或者以编程方式指定geckodriver.exe的位置,如下所示.

System.Environment.SetEnvironmentVariable("webdriver.gecko.driver",@"/path/to/geckodriver.exe"

注意:如果设置PATH环境变量,则可能需要重新启动系统.

从Firefox 47开始(不包括它),Selenium默认使用geckodriver功能.对于47及之前的版本,您可能需要关闭此功能,以便Selenium可以使用Firefox内置支持,就像我们以前使用这些版本一样.

JAVA版本实现相同:

DesiredCapabilities d = new DesiredCapabilities();
d.setCapability("marionette", false);  // to disable marionette.
WebDriver driver = new FirefoxDriver(d);
Run Code Online (Sandbox Code Playgroud)

参考文献:

  1. 如何在C#中设置系统属性
  2. https://msdn.microsoft.com/en-us/library/z46c489x.aspx
  3. https://superuser.com/questions/317631/setting-path-in-windows-7-command-prompt
  4. /sf/answers/2832627661/