73

I am trying to launch Mozilla but still I am getting this error:

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases

I am using Selenium 3.0.01 Beta version and Mozilla 45. I have tried with Mozilla 47 too. but still the same thing.

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
Reema
  • 969
  • 1
  • 10
  • 17

6 Answers6

101

The Selenium client bindings will try to locate the geckodriver executable from the system PATH. You will need to add the directory containing the executable to the system path.

  • On Unix systems you can do the following to append it to your system’s search path, if you’re using a bash-compatible shell:

    export PATH=$PATH:/path/to/geckodriver
    
  • On Windows you need to update the Path system variable to add the full directory path to the executable. The principle is the same as on Unix.

All below configuration for launching latest firefox using any programming language binding is applicable for Selenium2 to enable Marionette explicitly. With Selenium 3.0 and later, you shouldn't need to do anything to use Marionette, as it's enabled by default.

To use Marionette in your tests you will need to update your desired capabilities to use it.

Java :

As exception is clearly saying you need to download latest geckodriver.exe from here and set downloaded geckodriver.exe path where it's exists in your computer as system property with with variable webdriver.gecko.driver before initiating marionette driver and launching firefox as below :-

//if you didn't update the Path system variable to add the full directory path to the executable as above mentioned then doing this directly through code
System.setProperty("webdriver.gecko.driver", "path/to/geckodriver.exe");

//Now you can Initialize marionette driver to launch firefox
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new MarionetteDriver(capabilities); 

And for Selenium3 use as :-

WebDriver driver = new FirefoxDriver();

If you're still in trouble follow this link as well which would help you to solving your problem

.NET :

var driver = new FirefoxDriver(new FirefoxOptions());

Python :

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

caps = DesiredCapabilities.FIREFOX

# Tell the Python bindings to use Marionette.
# This will not be necessary in the future,
# when Selenium will auto-detect what remote end
# it is talking to.
caps["marionette"] = True

# Path to Firefox DevEdition or Nightly.
# Firefox 47 (stable) is currently not supported,
# and may give you a suboptimal experience.
#
# On Mac OS you must point to the binary executable
# inside the application package, such as
# /Applications/FirefoxNightly.app/Contents/MacOS/firefox-bin
caps["binary"] = "/usr/bin/firefox"

driver = webdriver.Firefox(capabilities=caps)

Ruby :

# Selenium 3 uses Marionette by default when firefox is specified
# Set Marionette in Selenium 2 by directly passing marionette: true
# You might need to specify an alternate path for the desired version of Firefox

Selenium::WebDriver::Firefox::Binary.path = "/path/to/firefox"
driver = Selenium::WebDriver.for :firefox, marionette: true

JavaScript (Node.js) :

const webdriver = require('selenium-webdriver');
const Capabilities = require('selenium-webdriver/lib/capabilities').Capabilities;

var capabilities = Capabilities.firefox();

// Tell the Node.js bindings to use Marionette.
// This will not be necessary in the future,
// when Selenium will auto-detect what remote end
// it is talking to.
capabilities.set('marionette', true);

var driver = new webdriver.Builder().withCapabilities(capabilities).build();

Using RemoteWebDriver

If you want to use RemoteWebDriver in any language, this will allow you to use Marionette in Selenium Grid.

Python:

caps = DesiredCapabilities.FIREFOX

# Tell the Python bindings to use Marionette.
# This will not be necessary in the future,
# when Selenium will auto-detect what remote end
# it is talking to.
caps["marionette"] = True

driver = webdriver.Firefox(capabilities=caps)

Ruby :

# Selenium 3 uses Marionette by default when firefox is specified
# Set Marionette in Selenium 2 by using the Capabilities class
# You might need to specify an alternate path for the desired version of Firefox

caps = Selenium::WebDriver::Remote::Capabilities.firefox marionette: true, firefox_binary: "/path/to/firefox"
driver = Selenium::WebDriver.for :remote, desired_capabilities: caps

Java :

DesiredCapabilities capabilities = DesiredCapabilities.firefox();

// Tell the Java bindings to use Marionette.
// This will not be necessary in the future,
// when Selenium will auto-detect what remote end
// it is talking to.
capabilities.setCapability("marionette", true);

WebDriver driver = new RemoteWebDriver(capabilities); 

.NET

DesiredCapabilities capabilities = DesiredCapabilities.Firefox();

// Tell the .NET bindings to use Marionette.
// This will not be necessary in the future,
// when Selenium will auto-detect what remote end
// it is talking to.
capabilities.SetCapability("marionette", true);

var driver = new RemoteWebDriver(capabilities); 

Note : Just like the other drivers available to Selenium from other browser vendors, Mozilla has released now an executable that will run alongside the browser. Follow this for more details.

You can download latest geckodriver executable to support latest firefox from here

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • 2
    Thanks you so much. This works just perfect. But just for information, what is this geckodriver all about? I have been working with selenium for quite sometime and i didn't come across this before. But now i had just recently change my machine and got this error. – Reema Jul 30 '16 at 23:12
  • 1
    @Reema Just like the other drivers available to Selenium from other browser vendors, Mozilla has released now an executable that will run alongside the browser. Have alook for more details..https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/IWebDriver – Saurabh Gaur Jul 31 '16 at 00:52
  • How to set path for geckodriver for all platforms? My Java app is built across different systems. – Michal Aug 13 '16 at 13:04
  • @Michal You need to set geckodriver location to your system PATH variable which valid for all platform...what is you OS?? – Saurabh Gaur Aug 13 '16 at 14:38
  • 2
    I decided to find another library that rarely make breaking changes like this. Seriously, after I updated the firefox or library, usually it will not works anymore. Even worse, the latest `geckodriver.exe` for windows only available for 64-bit system. It's time to say good bye to Selenium. – Thariq Nugrohotomo Aug 15 '16 at 09:49
  • @ThariqNugrohotomo Why is not works.. could you explain more??? Haw did you tried?? – Saurabh Gaur Aug 15 '16 at 15:47
  • 1
    I'm really confused. "The Selenium client bindings will try to locate the geckodriver" Is WebDriver turning into Geckdriver? What's going on here? – 8protons Aug 31 '16 at 20:17
  • 1
    @8protons Have a look here https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver/status...This geckodriver provides by Mozilla, .. geckodriver implements WebDriver So selenium clients bindings try to locate geckodriver to support automation on latest firefox – Saurabh Gaur Aug 31 '16 at 23:00
  • Its not working for me.I am using .net I added the directory of the driver in the path but still the same issue.I migrated to selenium 3.0.Is there anything else I need to update? – swathi Oct 14 '16 at 01:39
  • @swathi Could you share how did you add the directory of the the path?? – Saurabh Gaur Oct 14 '16 at 03:05
  • 1
    @SaurabhGaur able to solve it by getting the v0.11.1 version of gecko driver.. thank you :) – swathi Oct 14 '16 at 03:11
  • @SaurabhGaur Can you suggest the python alternative in this case to set a default download location. `profile = webdriver.firefox.firefox_profile.FirefoxProfile() profile.set_preference('browser.download.dir','D:\Downloads') driver = webdriver.Firefox(firefox_profile=profile)` – Surabhil Sergy Oct 17 '16 at 11:10
  • @SurabhilSergy [Have a look here](http://stackoverflow.com/questions/25251583/downloading-file-to-specified-location-with-selenium-and-python) – Saurabh Gaur Oct 17 '16 at 13:16
  • @SaurabhGaur Thanks. I have seen this already. I was looking the marionette driver way for that? [Similar question - looking for pythonic way](http://stackoverflow.com/questions/40086011/how-to-set-a-specific-download-location-in-mozilla-marionette-web-driver) – Surabhil Sergy Oct 17 '16 at 13:46
25
  1. Download gecko driver from the seleniumhq website (Now it is on GitHub and you can download it from Here) .
    1. You will have a zip (or tar.gz) so extract it.
    2. After extraction you will have geckodriver.exe file (appropriate executable in linux).
    3. Create Folder in C: named SeleniumGecko (Or appropriate)
    4. Copy and Paste geckodriver.exe to SeleniumGecko
    5. Set the path for gecko driver as below

.

System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
Gurusinghe
  • 512
  • 5
  • 12
Kanth25
  • 241
  • 3
  • 2
3

Selenium WebDriver Java code:

Download Gecko Driver from https://github.com/mozilla/geckodriver/releases based on your platform. Extract it in a location by your choice. Write the following code:

System.setProperty("webdriver.gecko.driver", "D:/geckodriver-v0.16.1-win64/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.lynda.com/Selenium-tutorials/Mastering-Selenium-Testing-Tools/521207-2.html");
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
0

Every Driver service in selenium calls the similar code(following is the firefox specific code) while creating the driver object

 @Override
 protected File findDefaultExecutable() {
      return findExecutable(
        "geckodriver", GECKO_DRIVER_EXE_PROPERTY,
        "https://github.com/mozilla/geckodriver",
        "https://github.com/mozilla/geckodriver/releases");
    }

now for the driver that you want to use, you have to set the system property with the value of path to the driver executable.

for firefox GECKO_DRIVER_EXE_PROPERTY = "webdriver.gecko.driver" and this can be set before creating the driver object as below

System.setProperty("webdriver.gecko.driver", "./libs/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
chuha.billi
  • 159
  • 1
  • 6
0

in my case, I must to set path in properties file, in many hours I find the way:

application.properties file:

webdriver.gecko.driver="/lib/geckodriver-v0.26.0-win64/geckodriver.exe"

in java code:

private static final Logger log = Logger.getLogger(Login.class.getName());
private FirefoxDriver driver;
private FirefoxProfile firefoxProfile;
private final String BASE_URL = "https://www.myweb.com/";
private static final String RESOURCE_NAME = "main/resources/application.properties"; // could also be a constant
private Properties properties;

public Login() {
    init();
}

private void init() {
    properties = new Properties();
    try(InputStream resourceStream = getClass().getClassLoader().getResourceAsStream(RESOURCE_NAME)) {
        properties.load(resourceStream);
    } catch (IOException e) {
        System.err.println("Could not open Config file");
        log.log(Level.SEVERE, "Could not open Config file", e);
    }
    // open incognito tab by default
    firefoxProfile = new FirefoxProfile();
    firefoxProfile.setPreference("browser.privatebrowsing.autostart", true);
    // geckodriver driver path to run
    String gekoDriverPath = properties.getProperty("webdriver.gecko.driver");
    log.log(Level.INFO, gekoDriverPath);
    System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + gekoDriverPath);
    log.log(Level.INFO, System.getProperty("webdriver.gecko.driver"));
    System.setProperty("webdriver.gecko.driver", System.getProperty("webdriver.gecko.driver").replace("\"", ""));
    if (driver == null) {
        driver = new FirefoxDriver();
    }

}
Community
  • 1
  • 1
nobjta_9x_tq
  • 1,205
  • 14
  • 16
0

I use from selenium-java-3.141.59 in windows 10 and solved my problem with this code:

System.setProperty("webdriver.gecko.driver", "C:\\gecko\\geckodriver.exe");
System.setProperty("webdriver.firefox.bin","C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();