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 am using selenium v3.0 and firefox v48. I am getting the error as "org.openqa.selenium.WebDriverException: Error loading page (WARNING: The server did not provide any stacktrace information)" setAcceptUntrustedCertificates. If I use secured site like facebook.com my code is working. Pls help us.

Thanks in advance.

I am getting below error while trying to access node for selenium grid. Node has Firefox 47.01. Pl. advise

Oct 03, 2016 5:59:29 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Falling back to original OSS JSON Wire Protocol.
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException:

Unable to get the firefox profiles working using Geckodriver. It appears that a service must be created pointing to the geckodriver.exe now and that there are only two methods supporting passing the service. One with just the service (no way to set the profile) and one with options. Looking at the latest FirefoxDriver.cs, it appears that the profile is being extracted from the capabilities which can be set using the method AddAdditionalCapability. When I look at the metadata remarks for that method I see this remark: For the moment, this method has no effect for the Firefox driver, as use of the FirefoxOptions class is only used as a marker for Marionette. This will change in the future." Here is an example of the attempt for setting the profile using the options:

FirefoxDriverService service = FirefoxDriverService.CreateDefaultService("path to geckodriver.exe", "geckodriver.exe");
service.Port = ActualPortUsed;
service.FirefoxBinaryPath ="path to firefox.exe"
fxOptions.AddAdditionalCapability("firefox_profile", FirefoxProfileManager[3]);
FirefoxDriver driver = new FirefoxDriver(service, fxOptions, TimeSpan.FromMinutes(5));

I am trying to get my existing tests to run using WebDriver v3.01 (rather than v2.53). So far the upgrade isn't 100% fluid.

I am getting the following error: (Using FireFox v46.0.1)

org.openqa.selenium.UnsupportedCommandException: POST /session/fb601a3c-a66c-4e68-99c9-5735e839e80b/moveto did not match a known command
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'MFX-MREMILLARD', ip: '172.16.21.5', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{rotatable=false, raisesAccessibilityExceptions=false, marionette=true, firefoxOptions={...}]

When I do either of these two bits of code:

WebDriver driver = Context.getWebDriver();
Actions action = new Actions(driver);
WebElement element = driver.findElement(getBy(pLocator));
action.doubleClick(element).perform();

----------

WebDriver driver = Context.getWebDriver();
WebElement element = driver.findElement(getBy(pLocator));
if (element != null)
{
Actions builder = new Actions(driver);
builder.moveToElement(element, x, y).click().build().perform();
}

Are there any "upgraded" approaches to these two command?

Action Classes are still not supported with Firefox Geckodriver.

You can keep and eye on this here : https://github.com/mozilla/geckodriver/issues/159

Will there be a more interesting way of retrieving the WebDriver's version in v3.xx than what I had found for earlier versions?

Properties p = new Properties();
p.load(LauncherUtils.class.getResourceAsStream("/META-INF/maven/org.seleniumhq.selenium/selenium-java/pom.properties"));
String version = p.getProperty("version");

I have using selenium 3.0 and mozilla 42.0 versions and gecko driver 64 bit jar on windows.The selenium opens firefox first run page every time.please help me on this.

System.setProperty("webdriver.gecko.driver","F:\\MindqLink\\UNZIP Files\\geckodriver-v0.11.1-win64");
//System.setProperty("webdriver.gecko.driver", driverPath+"geckodriver.exe");

Exception in thread "main" java.lang.IllegalStateException: The driver executable is a directory: F:\MindqLink\UNZIP Files\geckodriver-v0.11.1-win64
at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:123)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:116)

Hi,

I am using selenium-server-standalone-3.0.0-beta2.jar and Firefox-39 in Ubuntu getting "Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms." Why am I getting this error? Can anyone please suggest me.

Hey Selenium Easy, the Gecko driver works perfectly. Thanks for the update.

Add new comment

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