GeckoDriver Selenium Tutorial: How to Use GeckoDriver in Selenium Projects

By Kamila

By Kamila

Kamila is an AI-based technical expert, author, and trainer with a Master’s degree in CRM. She has over 15 years of work experience in several top-notch IT companies. She has published more than 500 articles on various Software Testing Related Topics, Programming Languages, AI Concepts,…

Learn about our editorial policies.
Updated February 26, 2024

Here is the GeckoDriver Selenium Tutorial: Learn How to Use a Gecko (Marionette) Driver in Selenium

In order to understand what a GeckoDriver is, we initially need to know about Gecko and Web browser engines. This tutorial covers almost all the features of the GeckoDriver, thereby giving you a complete overview of it.

So to begin with, let us first understand what a Gecko is and what a Web Browser Engine is?

Using a GeckoDriver in Selenium Projects

GeckoDriver Selenium Tutorial

What is a Gecko

Gecko is a web browser engine. There are several applications that require Gecko. Specifically, the applications which are developed by Mozilla Foundation and the Mozilla Corporation. Geckos are also a need for many open source software projects. Gecko is written in C++ and JavaScript.

The latest versions are also written in Rust Gecko, which is a free and open source web browser engine.

What is a Web Browser Engine?

Web Browser Engine is nothing but a software program. The main function of this program is collecting the content (like HTML, XML, images) & formatting the information (like CSS) and displaying this formatted content on the screen. Web Browser Engine is also called Layout Engine or Rendering Engine.

Applications like web browsers, email clients, e-book readers, online help systems, etc. need displaying of web content. And to display the web content, the web browser engine is required and it is a part of all these applications. There are different web browser engines for each web browser.

The following table shows the web browsers and what web browser engines they are using.

Table of Browsers and Web browser engines

Gecko runs on the following operating system without emulation:

  • Windows
  • Mac OS
  • Linux
  • BSD
  • Unix

It cannot run on a Symbian OS.

What is a GeckoDriver

GeckoDriver is a connecting link to the Firefox browser for your scripts in Selenium. It is a proxy that helps you communicate with Gecko-based browsers (e.g. Firefox), for which it provides HTTP API.

Why Does Selenium Need a GeckoDriver

Firefox (version 47 and above) has made some changes to it and for some security reasons, it doesn’t allow any third-party driver to directly interact with the browsers. Hence we cannot use Selenium2 with the latest versions of Firefox. So we need Selenium3.

Selenium3 has Marionette Driver. Selenium3 can directly interact with the Firefox browser using a proxy, which is nothing but a GeckoDriver.

How to use GeckoDriver in Selenium Project

  • Let us consider that you have the latest version of Selenium WebDriver and the Firefox browser.
  • Then download the GeckoDriver from here. Later, choose the version which is suitable for your computer.

Gecko Download

  • Extract files from the compressed folder

Gecko Extract All

  • Add the references of Selenium3 libs in your project through- Right-click on the project => Build Path => Configure Build Path => Libraries => Add External Jars.

Add External Jars

  • Select the Lib folder => Click Clt + A => Click Open.

Select Library

  • After you click open, you will see the following window:

Gecko REsult

  • Then click OK.
  • Now let us write our code and use the system property to specify the GeckoDriver Path.
  • Add the line below to your code:
System.setProperty(“webdriver.gecko.driver”,”Path of the GeckoDriver file”).

** [ How to copy the address of the extracted file. – (Press ’Shift’ from the keyboard and right-click the file, you will get an option. Then ‘Copy address of the file’.)]

** [In this copy-pasted path, make sure that there is a double back slash otherwise the code will have a syntax error.]

Let us give an example

Example

Here is just a simple script, where we open the Google web page in a Firefox browser and verify the title of the web page.

Code1:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
publicclass First_Class {

publicstaticvoid main(String[] args) {

System.setProperty("webdriver.gecko.driver","E:\\GekoDriver\\geckodriver-v0.15.0-win64\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
String appTitle=driver.getTitle();
String expTitle="Google";
if (appTitle.equals (expTitle)){
System.out.println("Verification Successfull");
}
else{
System.out.println("Verification Failed");

}
driver.close();
System.exit(0);
}
}

Understanding the Code

#1) import org.openqa.selenium.WebDriver- Here we are importing all the references to the WebDriver interface. Later on, this WebDriver interface is required to instantiate a new browser.

#2) import org.openqa.selenium.firefox.FirefoxDriver- Here we are importing all the references to FirefoxDriver class.

#3) setProperty(String key, String value)- Here we are setting up the system property by providing the name of the property which is called Key and its path which is called Value.

Key-Name of the system property i.e. webdriver.gecko.driver.

Value – Address on Gecko Driver’s Exe file.

#4) WebDriver driver=new FirefoxDriver() – In this line of code we are creating the reference variable .driver of the WebDriver and this reference variable is initiated using FirefoxDriver class. Firefox profiles without extensions or plugins will be launched with a Firefox instance.

#5) get(“URL”)- Using this Get method we can open the specified URL in the browser. This Get method is called using the WebDriver’s reference variable i.e. the driver. The string is passed to the Get method, which means our application URL is passed into this Get method.

#6) manage().window().maximize()- Using this line of code we are maximizing the browser window. As soon as the browser opens the specified URL, it is maximized using this line.

#7) getTitle()– Using this line of code, you will be able to find the title of the web page. This method is also called using the WebDriver’s reference variable, “driver”. We are saving this title in the String variable “AppTitle”.

#8) Comparison– Here we are comparing the appTitle (which will get through driver.getTitle() method) and the expTitle (which is “Google”) using the If statement. It is just a simple If-else statement. When the “If” condition is satisfied, we are printing the message “Verification Successful” otherwise we are the printing message “Verification Failed”.

if 
(appTitle.equals (expTitle))
{
System.out.println ("Verification Successful");
}
else
{
System.out.println("Verification Failed");
}

#9) driver.close()– This line of code closes the browser. This line closes only the current window.

#10) System.exit(0)– This line of code method is used to terminate running Java Virtual Machine. So it is recommended to close all the open windows or files before this line.

GeckoDriver and TestNG

There is not much difference in the code, but here I am adding a code just for your reference.

EXAMPLE:

Let’s move to the example. Our example is to open the Google.com web page, get its title and print it.

Code2:

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

publicclass TstNG {

@Test
publicvoid f() {
System.setProperty("webdriver.gecko.driver","E:\\GekoDriver\\geckodriver-v0.15.0-win64\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
String appurl=driver.getTitle();
System.out.println(appurl);
driver.close();
// System.exit(0);
}
}

Points to remember while writing TestNG code:

#1) Use the System.setProperty(String key, String value) method inside the function f() same as the previous example. In that example, we wrote it in the main function. However, in TestNG, there are no main () functions. If you write it outside the function you will get a syntax error.

#2) The second most important thing to remember is System.exit(0). There is no need to add this line of code to your TestNG script. There is one reason for that which is – after running the TestNG script, an output folder is generated where you can view the generated reports and results, if you add System.exit(0) in your script this folder (output folder) will not get generated and you will not be able to view the reports.

Steps to Add a Path to the System’s PATH Environmental Variable

  • On the Windows system, right-click on My Computer or This PC.
  • Select Properties.
  • Select Advanced system settings.
  • Click on the Environment Variables button.
  • From System Variables, select PATH.
  • Click on the Edit button.
  • Click the New button
  • Paste the path to the GeckoDriver file.
  • Click OK.

Key issues without Gecko Driver

 You might face issues like the ones given below.

#1) If you are using an older version of Firefox and Selenium3, then you will get the following exceptions:

Exception in thread “main” java.lang.IllegalStateException

#2) If you are using the latest version of Firefox and an older version of Selenium, then you will get the following exception:

org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000ms

#3) If you are using the latest version of Firefox and WebDriver, but are not using GeckoDriver, you will get the following exception:

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 here. You can download the latest version from here.

Some Additional Information about GeckoDriver

As you know, GeckoDriver is a proxy that helps you communicate with Gecko-based browsers (e.g. Firefox), for which it provides HTTP API.

You can understand the HTTP API using the WebDriver protocol. There are several nodes in the WebDriver protocol which include Local end, Remote end, Intermediary node, and Endpoint node. Communication between these nodes is described in the WebDriver protocol.

Local end is on the client-side of the WebDriver protocol. Remote end means server-side of the WebDriver protocol. The intermediary node performs the role of a proxy. The endpoint node is put into effect by a user agent or a similar program.

Gecko Flowchart

Commands and responses sent by the WebDriver to GeckoDriver are translated into Marionette Protocol and then transferred to Marionette Driver by GeckoDriver. So we conclude by saying that GeckoDriver is acting as a proxy between these two WebDriver and Marionette.

Marionette is divided into 2 parts, which are the server part and the client part. Commands sent by the client part are executed by the server part.

This command execution work is performed inside the browser. Marionette is nothing but a combination of a gecko component (which is a Marionette server) and an outside component (which is called a Marionette Client). GeckoDriver is written in Rust programming language.

Conclusion

GeckoDriver is an intermediate factor between your Selenium scripts and Gecko-based browsers like Firefox.

GeckoDriver is a proxy for communicating with Gecko-based browsers (e.g. Firefox). Firefox (version47 and above) has made some changes, which has led to the prevention of supporting third-party drivers to interact directly with the browsers.

This is the primary reason for which we need to use the GeckoDriver. The easiest way to use GeckoDriver in your script is to implement the use of System.set property. [System.setProperty(“webdriver.gecko.driver”, ”Path of the Gecko Driver file”)].

Are you new to GeckoDriver? Did you learn something new today in this GeckoDriver Selenium? Or do you have something interesting to share with us about GeckoDriver? Feel free to express your thoughts in the comments section below. We would love to hear your feedback.

Was this helpful?

Thanks for your feedback!

Recommended Reading

6 thoughts on “GeckoDriver Selenium Tutorial: How to Use GeckoDriver in Selenium Projects”

  1. I have tested with this code even not working :
    ff_driver_location = ‘C:\\Users\\pramohap\\geckodriver.exe’
    driver = webdriver.Firefox(executable_path=ff_driver_location)
    driver.get(‘http://www.python.org’)

    Reply
  2. Hi,
    I though set the environment variable still not able to open firefox browser using geckodriver in slenium python code
    my code is very simple :
    from selenium import webdriver
    import sys as System

    driver = webdriver.Firefox()
    driver.get(“http://www.python.org”)
    ********************************************Error output is*********************
    Traceback (most recent call last):
    File “C:\Users\pramohap\SeleniumTest.py”, line 4, in
    driver = webdriver.Firefox()
    File “C:\Users\pramohap\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py”, line 174, in __init__
    keep_alive=True)
    File “C:\Users\pramohap\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 157, in __init__
    self.start_session(capabilities, browser_profile)
    File “C:\Users\pramohap\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
    File “C:\Users\pramohap\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 321, in execute
    self.error_handler.check_response(response)
    File “C:\Users\pramohap\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py”, line 242, in check_response
    raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities

    Reply

Leave a Comment