How to use the gecko executable with Selenium

160,914

Solution 1

Recently Selenium has launched Selenium 3 and if you are trying to use Firefox latest version then you have to use GeckoDriver:

System.setProperty("webdriver.gecko.driver","G:\\Selenium\\Firefox driver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();

You can check full documentation from here

Solution 2

You can handle the Firefox driver automatically using WebDriverManager.

This library downloads the proper binary (geckodriver) for your platform (Mac, Windows, Linux) and then exports the proper value of the required Java environment variable (webdriver.gecko.driver).

Take a look at a complete example as a JUnit test case:

public class FirefoxTest {

  private WebDriver driver;

  @BeforeClass
  public static void setupClass() {
    WebDriverManager.firefoxdriver().setup();
  }

  @Before
  public void setupTest() {
    driver = new FirefoxDriver();
  }

  @After
  public void teardown() {
    if (driver != null) {
      driver.quit();
    }
  }

  @Test
  public void test() {
    // Your test code here
  }
}

If you are using Maven you have to put at your pom.xml:

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>5.1.0</version>
</dependency>

WebDriverManager does magic for you:

  1. It checks for the latest version of the WebDriver binary
  2. It downloads the WebDriver binary if it's not present on your system
  3. It exports the required WebDriver Java environment variables needed by Selenium

So far, WebDriverManager supports Chrome, Opera, Internet Explorer, Microsoft Edge, PhantomJS, and Firefox.

Solution 3

I am also facing the same issue and got the resolution after a day :

The exception is coming because System needs Geckodriver to run the Selenium test case. You can try this code under the main Method in Java

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

For more information You can go to this https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver link.

Please let me know if the issue doesn't get resolved.

Solution 4

The solutions above work fine for local testing and firing up browsers from the java code.If you fancy firing up your selenium grid later then this parameter is a must have in order to tell the remote node where to find the geckodriver:

-Dwebdriver.gecko.driver="C:\geckodriver\geckodriver.exe"

The node cannot find the gecko driver when specified in the Automation Java code.

So the complete command for the node whould be (assuming node and hub for test purposes live on same machine) :

java -Dwebdriver.gecko.driver="C:\geckodriver\geckodriver.exe" -jar selenium-server-standalone-2.53.0.jar -role node -hub http://localhost:4444/grid/register

And you should expect to see in the node log :

00:35:44.383 INFO - Launching a Selenium Grid node
Setting system property webdriver.gecko.driver to C:\geckodriver\geckodriver.exe

Solution 5

I try to make it simple. You have two options while using Selenium 3+:

  • Either upgrade your Firefox to 47.0.1 or higher and use the default geckodriver of Selenium3.

  • Or disable using of geckodriver by specifying marionette to false and use the legacy Firefox driver. a simple command to run selenium is: java -Dwebdriver.firefox.marionette=false -jar selenium-server-standalone-3.0.1.jar. You can also disable using geckodriver from other commands that are mentioned in other answers.

Share:
160,914
Robert Reynolds
Author by

Robert Reynolds

Updated on July 09, 2022

Comments

  • Robert Reynolds
    Robert Reynolds almost 2 years

    I'm using Firefox 47.0 with Selenium 2.53. Recently they have been a bug between Selenium and Firefox which make code not working. One of the solution is to use the Marionnette driver.

    I followed the instruction of this site to use this new driver with a RemotWebDriver but I keep having the error :

    WARN - Exception: Exception in thread "main" org.openqa.selenium.WebDriverException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/jgraham/wires. The latest version can be downloaded from ....

    The code i've tried so far is very simple :

    public class Test {
        static WebDriver driver;
        static Wait<WebDriver> wait;
        public static void main(String[] args) throws MalformedURLException {
            System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\geckodriver.exe");
            DesiredCapabilities cap = DesiredCapabilities.firefox();
            cap.setCapability("marionette", true);
            cap.setBrowserName("firefox");
            driver = new RemoteWebDriver(new URL("http://192.168.117.135:5555/wd/hub"), cap);//true to enable the JS
            wait = new WebDriverWait(driver, 3000);
            final String url = "https://www.google.com/";
    
            JavascriptExecutor js = (JavascriptExecutor) driver;
    
            try {
                driver.navigate().to(url);
            } finally {
                driver.close();
            }
        }
    }
    

    I'm sure that the path to the geckodriver.exe is right and i don't see where i did the mistake.

    EDIT 1: I tried the following code :

    public class Test {
        static WebDriver driver;
        static Wait<WebDriver> wait;
        public static void main(String[] args) throws MalformedURLException {
            System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\geckodriver.exe");
    
            driver = new MarionetteDriver();
            wait = new WebDriverWait(driver, 3000);
            final String url = "https://www.google.com/";
    
            JavascriptExecutor js = (JavascriptExecutor) driver;
    
            try {
                driver.navigate().to(url);
            } finally {
                driver.close();
            }
        }
    }
    

    and it's working it seems that the problem come from the RemoteWebDriver and the gecko driver, any of you have news on it ?

  • Brick
    Brick over 7 years
    This solution is clearly tied to Windows (because of the path). Is there a platform independent way to do this, or does it need to be set for each machine that going to run Selenium 3 code?
  • Tomislav Nakic-Alfirevic
    Tomislav Nakic-Alfirevic over 7 years
    What we tend to do is have a conf. file which is environment-specific (test machine IP, various ports, paths etc.) and use it as such, but no, I wasn't very happy with having to specify a path to an executable to be able to run a test. If anyone has a better idea, I would appreciate being enlightened.
  • mike rodent
    mike rodent over 7 years
    Thanks... I'm getting Caused by: java.net.ConnectException: Connection refused: connect on your 4th line here... any idea what that might be about?
  • Purendra Agrawal
    Purendra Agrawal over 7 years
    As per my understanding, ConnectException will come when the connection was refused remotely. Did you give the correct path for the geckodriver in the line1?
  • mike rodent
    mike rodent over 7 years
    Hah! Thanks... got it working eventually. Thanks for spending a day of your precious life on this... I wonder what all this business of "gecko" and "marionette" and "capability" is all about? And why it's so vital that you have to jump through this hoop to use Selenium 3.0.0.?
  • dmgig
    dmgig over 7 years
    Thanks, this worked for me trying to set up codeception on Debian, with iceweasel installed.
  • voji
    voji about 7 years
    The given syntax throw an exception with newer selenium-server versions (like 3.2.0): Exception in thread "main" com.beust.jcommander.ParameterException: Unknown option: -Dwebdriver.gecko.driver=geckodriver.exe at com.beust.jcommander.JCommander.parseValues(JCommander.java:‌​742) ... To avoid this, you need to change the arguments order (property declaration first, jar, and program args. after). Example: java -Dwebdriver.gecko.driver="./geckodriver.exe" -jar selenium-server-standalone-3.2.0.jar -role node -hub 10.64.201.100:4444/grid/register
  • ChayanC
    ChayanC almost 7 years
    I have tried with given set property line, but I am not able to launch the Firefox browser in mac machine.
  • Valya
    Valya over 6 years
    not a single word about the geckodriver.exe, which should be used the same way Mukesh otwani's answer suggests.
  • clockworks
    clockworks over 6 years
    for mac you can download webdriver from here: github.com/mozilla/geckodriver/releases. Also set property like this System.setProperty("webdriver.gecko.driver", "/Users/gecko/geckodriver");
  • invzbl3
    invzbl3 over 4 years
    I don't recommend to use 3.3.1 Selenium driver because of specific issues. To avoid it better to use 3.4.0.