Search code examples
c#seleniumselenium-webdriverwebdriverselenium3

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


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

Solution

  • 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