How To Launch Firefox Browser in Selenium WebDriver – Java

Selenium automates a browser. There are multiple browsers supported by Selenium WebDriver. Major browsers are – Google Chrome, Mozilla Firefox, Microsoft Internet Explorer, Microsoft Edge, Opera, and Safari. In this post, we will learn to “Launch a firefox browser in Selenium WebDriver 3 and 4“.

Install Firefox browser

First, check if you have firefox browser installed in your system. If it is installed then jump to the next step otherwise install the firefox browser from here. Installation is just like any other software installation. Click on the installation file and Next Next Next……

Setup a Selenium WebDriver Java project

Since we will use Java binding of Selenium WebDriver to launch a firefox browser, please set up a Selenium Java project as discussed here.

Use FirefoxDriver class of Selenium WebDriver

Selenium WebDriver provides corresponding classes for browsers. For example:- For Chrome -> ChromeDriver, For Firefox -> FirefoxDriver. To launch any browser through Selenium WebDriver we just need to create an object of their corresponding class.

Selenium – Java Code

package BasicSeleniumConcepts;

import org.openqa.selenium.firefox.FirefoxDriver;

public class LaunchFirefoxBrowser {
	
	public static void main(String[] args) {
		
		FirefoxDriver firefoxDriver = new FirefoxDriver();
		firefoxDriver.get("https://www.google.com/");
	}

}

Run above program and observe the output.

We were expecting that a firefox browser should have launched but we got an exception IllegalStateException and states that “The path to the driver executable must be set by the webdriver.gecko.driver system property“.

Selenium WebDriver can not communicate with a browser directly. We need an intimidatory executable agent between Selenium WebDriver and the actual browser. These intimidatory are provided by third parties. For firefox, we need a geckodriver as an interface between Selenium WebDriver and a real firefox browser.

There is a critical part that for each version of the Firefox browser, you may have different geckodriver executable version. Many times you may face issues when Selenium will not be able to create a WebDriver session and maximum time reason is an incompatibility of Firefox browser version and geckodriver version.

I will advised you to download the latest firefox browser and geckodriver always. If you are using an older version of Firefox due to some restrictions always read release notes to know which geckdriver version will support your current version of firefox browser.

I have installed the below version of the Firefox browser on my Windows machine of 64 bits. You need all these details to download proper geckodriver executable file.

Let’s find a compatible version of geckodriver for my installed version of firefox. Remember this is just for reference. These versions will be changed with time. So just to follow the above process and download the correct result.

I will download for Windows – 64 bits. You can refer to the names below to distinguish. For windows, it has “win64” or “win32” in names. Just click on link and it will start downloading. It is a zipped file which you need to unzip. After unzip you will get a geckodriver.exe file as exe is extension of exeutable file on Windows. For Mac and Linux there will be no extension as they follow permission based capability to identify an executable file.

Note:- You can rename this file whatever you want. There is no restrictions that you can not change its name. Many beginners get confuse on this. You just need to use changed name in your program.

Before launching a Firefox browser you need to let WebDriver know explicitly the location of geckodriver executable file. As of now, we will add the path of geckodriver as a system property. There are some other ways as well which we will see later. Any Sytem property is set as a key value pair and to set path of geckodriver, selenium WebDriver has a key as “webdriver.gecko.driver”. Remeber you can not set key to some other name.

Remember we need to set a path of geckodriver with the extension of it if available. It is needed for Windows but for Linux and Mac you need to give it executable permission.

Selenium – Java Code

package BasicSeleniumConcepts;

import org.openqa.selenium.firefox.FirefoxDriver;

public class LaunchFirefoxBrowser {
	
	public static void main(String[] args) {
		
		// Set the path of geckodriver with extension for windows
		System.setProperty("webdriver.gecko.driver", "C:\\Users\\amomahaj\\Downloads\\geckodriver-v0.26.0-win64 (1)\\geckodriver.exe");
		FirefoxDriver firefoxDriver = new FirefoxDriver();
		firefoxDriver.get("https://www.google.com/");
	}

}

Now you will be able to launch Firefox browser successfully and it will launch Google.

Do not worry about seeing these red lines. They are just logs. You can ignore as of now. It has some importamt message which will see later.

You can download/clone the above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

6 thoughts on “How To Launch Firefox Browser in Selenium WebDriver – Java

  1. I have observed the quit option is not working several times, how can we fix the quit function issue for Firefox driver?

    1. Hi Uma,

      It is quite a problem in firefox. Sometimes close also does not work. You can use javascript way to close a window. Command is “window.close()”.
      Thanks
      Amod

  2. @Amod…Thank you for your useful post
    Here are some doubts,
    Is it useful if we start using selenium 3 instead other lesser versions??
    Is is necessary to adopt selenium 3??

Leave a Reply to Amod Mahajan Cancel reply

Your email address will not be published. Required fields are marked *