How to set up selenium 3.0, getting error “The geckodriver.exe file does not exist…” in c#

耗尽温柔 提交于 2019-12-01 01:26:55

问题


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.

When I'm using remote mode (seleniumhub), it works fine even if it uses firefox 45.0 version.

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();
                }

回答1:


From selenium 3.0, you have to use the geckodriver for Firefox browser.

download the latest geckodriver from here https://github.com/mozilla/geckodriver/releases

You have two options:

  1. enter geckodriver path in Windows System Environment Variable PATH.
  2. Or specify the location of the geckodriver.exe programmatically as follows.

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

Note: System restart may be required if you set PATH environment variable.

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 version to achieve the same:

DesiredCapabilities d = new DesiredCapabilities();
d.setCapability("marionette", false);  // to disable marionette.
WebDriver driver = new FirefoxDriver(d);

References:

  1. how to set system properties in 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. https://stackoverflow.com/a/40466109/2575259



回答2:


I a similar issue with the Chrome driver for Selenium, I was following along a course on building an automation framework and I installed the NuGet package under framework references, instead of installing it under tests.




回答3:


You can download geckodriver from here: https://github.com/mozilla/geckodriver/releases, and then you just need to add a directory of the file into FirefoxDriver constructor like this:
new FirefoxDriver("geckoDriverDirectory")



来源:https://stackoverflow.com/questions/40657443/how-to-set-up-selenium-3-0-getting-error-the-geckodriver-exe-file-does-not-exi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!