Selenium 3 - Launching firefox browser using Geckodriver

The first example that we will look into is launching firefox using the Geckodriver. When using Selenium 3 , you have to download geckodriver. Just like the other drivers available to Selenium, Mozilla has released geckodriver executable that will run alongside the browser.

You can download the latest executable on the GitHub page. Now you need to specify the system property with the path

System.setProperty("webdriver.gecko.driver","path of geckodriver.exe");
WebDriver driver = new FirefoxDriver();

Below is the code to set GeckoDriver path on Mac OS X for Selenium WebDriver

System.setProperty("webdriver.gecko.driver", "/Users/username/Downloads/geckodriver");
WebDriver driver = new FirefoxDriver();

What is GeckoDriver?

A Proxy for using W3C WebDriver-compatible clients to interact with Gecko-based browsers. Geckodriver provides HTTP API described by the WebDriver protocol to communicate with Gecko browsers, such as Firefox (Version after 47).

Marionette (the next generation of FirefoxDriver) is turned on by default from Selenium 3. Even if you are working with older versions of Firefox browser, Selenium 3 expects you to set path to the driver executable by the webdriver.gecko.driver.

Click here For more details on Marionette

Note: If you are using Selenium version below 2.xx, you don't need gecko additional driver. You can downloaded selenium-server-standalone-2.53.1.jar from download selenium 2.53.1

If you are not doing so, it will throw exception "java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property;"

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
	at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
	at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
	at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:38)
	at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:91)
	at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
	at org.openqa.selenium.firefox.FirefoxDriver.createCommandExecutor(FirefoxDriver.java:245)
	at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:220)
	at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:215)
	at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:211)
	at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:124)
The other important changes in Selenium 3.x are listed below:

* Minimum java version is now 8+
* The original RC APIs are only available via the leg-rc package.
* To run exported IDE tests, ensure that the leg-rc package is on the classpath.
* Support for Firefox is via Mozilla's geckodriver.
* Support for Edge is provided by MS:
* Official support for IE requires version 9 or above
* New html-table runner backed by WebDriver.
* Unused command line arguments are now no longer parsed.

Now let us see the example to launch firefox browser with Selenium 3 using gecko driver.

package com.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class ExampleThree {
	
	String driverPath = "<path to gecko driver executable>";
	public WebDriver driver;
	
	@Test
	public void launchBrowser() {
		System.out.println("launching firefox browser"); 
		System.setProperty("webdriver.gecko.driver", driverPath+"geckodriver.exe");
		driver = new FirefoxDriver();
	}

	@Test
	public void openApplication() {
		driver.navigate().to("http://www.google.com");
	}
	
	@Test
	public void closeDriver() {
		if(driver!=null) {
			driver.close();
		}
	}
}

To run tests on remote machines, WebDriver has to use the instance of the RemoteWebDriver and DesiredCapabilities in order to specify browser name, version and platform to execute tests.

Generally to run tests on our local machine, we will just specify as WebDriver driver = new FirefoxDriver(); to run on Firefox browser.

To execute tests on remote machine, we need to use remotewebdriver, below is the sample code to execute your tests on remote machine with Firefox gecko driver

System.setProperty("webdriver.gecko.driver", driverPath+"geckodriver.exe");
DesiredCapabilities capabilities=DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);

The above code is verified with selenium-server-standalone-3.0.0-beta2 and Firefox 48 version. Please let me know if you have any issues.

In your Maven project, just add / update the following selenium dependency to your pom.xml:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.0.0-beta2</version>
    </dependency>  

When working with selenium grid and if you want your tests to run on Firefox with Selenium version 3.x.x, then we need to provide path to gecko driver executable. Detailed information on Selenium Nodes configuartion using JSON for Firefox gecko driver

The most common issue people are facing with latest versions of Firefox is org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms.

Users who are facing the above problem, Please use Marionette (geckodriver).

Please do comment your issue / observation with the versions (Selenium, Firefox and Geckodriver) that you have used.

UPDATE on geckodriver windows 32bit issues

Sep 1, 2016 3:07:19 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end

If you are facing an issue like org.openqa.selenium.remote.ProtocolHandshake createSession with geckodriver windows 32bit , Please check for an update of the issue here and check the last comment made by 'Andreas Tolfsen'. Once all the issues fixed in the milestone they will soon be releasing Geckodriver v0.11.

UPDATE:
Now you should be able to download gecko driver version 0.11 which will resolve windows32 bit issues.

Selenium Tutorials: 

Comments

Hi,

I have fireFox version - 45.6.0 and selenium-server-standalone-3.0.1.jar. But getting 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

Code as below
System.setProperty("webdriver.gecko.drive", "D:/Harish/Learning/ActiTime\\src/drivers/geckodriver.exe");
drive = new FirefoxDriver();

I am using selenium 3.0.1 with gicko driver "geckodriver-v0.13.0-win32" and FF 51.0.1. I am facing "Failed to start up socket within 45000 milliseconds. Attempted to connect to the following addresses: 127.0.0.1:7055" when i am run tset cases with firefox.

I am not able to mouse hover on an element on the page and get this error.
Exception in thread "main" org.openqa.selenium.UnsupportedCommandException: POST /session/bdda6d06-8b61-c142-8d86-a9f569ab6d96/moveto did not match a known command.

I am using selenium 3.1.0 with firefox 51.0.1. I have written a test script for google search.
While running this code i am getting error: org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms.

Very Good topics...and and very simple grammer to understand..thanks lot

Hi
I am using eclipse ide with java 8 and selenium 3..the firefox instance is called but its not fetching the url ,in the driver.get(url) command.i have also tried driver.navigate().to(url).but still its not being opened.

org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS

Downloaded gecko version 0.15 and try in firefox with below code:
System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");
driver = new FirefoxDriver();
Result is coming but before result getting above error.

I get this error " syntax error near unexpected token `"webdriver.gecko.driver","/Users/username/Downloads/geckodriver"' when I use this command in terminal:
System.setProperty("webdriver.gecko.driver","/Users/username/Downloads/geckodriver");

Hints as to why this is? Using Mac OS Sierra, Firefox 53.0, geckodriver 0.16.1, selenium 3.4.

unable to launch the firefox :

Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: VISTA
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'PREM', ip: '10.134.10.22', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
Driver info: driver.version: FirefoxDriver
at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:115)

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.