-3

I was getting the error:

The path to driver executable must be set by the webdriver.gecko.driver system property

Then i downloaded geckodriver and set the path as below. After that the code is working fine.

But my question is do i need to use the below code every time (in each and every program) when i want to launch a firefox using selenium code?

System.setProperty("webdriver.gecko.driver","<path to geckodriver.exe>");
WebDriver driver = new FirefoxDriver(); 
Sandeep Khade
  • 2,832
  • 3
  • 21
  • 37
  • 1
    Yes, that code must execute every time your program runs. Have you tried running it without? Would get your answer by trying it out – Vince Aug 10 '17 at 15:10
  • 1
    You can set this property using command line option "-Dproperty=value". java -Dwebdriver.gecko.driver="path" SomeClass. Then it will be available for classes in the JVM and no need to write this line in your code. You can set up the variable in the IDE like eclipse pretty easily. – Grasshopper Aug 10 '17 at 16:31

3 Answers3

2

When we work with Selenium 3.x, geckodriver and Mozilla Firefox Browser through Selenium-Java bindings we need to configure the Test Environment through System.setProperty line. Find the details along with your Answer below.

Your Question have 2 parts so I will answer both of them in parts:


1. Do I need to use the below code every time in each and every Program:

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

Answer:

Yes

Explaination:

Whenever we need to execute a program (Selenium-Java based) it is mandatory we explicitly mention the type of driver (gecko, chrome, ie) which we are trying to use in our program in the form of "webdriver.gecko.driver". Along with it, we also need to explicitly mention the absolute path of the driver (gecko, chrome, ie) binary (.exe) in the form of "<path to geckodriver.exe>". Next we are using the WebDriver interface and casting the WebDriver instance to FirefoxDriver.


2. Do I need to use the below code every time when i want to launch Firefox:

System.setProperty("webdriver.gecko.driver","<path to geckodriver.exe>");

Answer:

No

Explaination:

Once we configure the WebDriver instance i.e. the driver through DesiredCapabilities class, the driver is able to carry the same configuration till its life time which is controled through your Automation Script. So until and unless we explicitly call quit() method through the driver, the driver instance remains active and carries the configuration. So, within your program no matter how many time you choose to close the browser instance by calling close() method, you can always mention driver = new FirefoxDriver(); to open a new browser session again and again with the stored configuration within the driver.

An Example:

package demo;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Driver_Close_Initiate 
{
    static WebDriver driver;

    public static void main(String[] args) 
    {
        System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
        DesiredCapabilities dc = DesiredCapabilities.firefox();
        dc.setCapability("marionette", true);
        driver =  new FirefoxDriver(dc);
        driver.get("https://google.com");
        driver.close();
        driver =  new FirefoxDriver(dc);
        driver.get("https://facebook.com");
        driver.quit();
    }
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

The reason you need to use the gecko driver is your Firefox version is greater than v47. So to answer your question, if you don't want to use the gecko driver everytime, you need to downgrade your Firefox version to 47 or below.

Previous versions can be found here:

https://ftp.mozilla.org/pub/firefox/releases/

IamBatman
  • 975
  • 12
  • 18
0

Yes you have to use setProperty(String key, String path) every time if you want to use Firefox version above 47 and selenium jars above 3.0.

System.setProperty("webdriver.gecko.driver", "Path of geckodriver.exe");

or, you can set the configurtion path using DesiredCapabilities classs

DesiredCapabilities des_capablity = DesiredCapabilities.firefox();
des_capablity.setCapability("marionette", true);
driver =  new FirefoxDriver(des_capablity);
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36