Search code examples
javaseleniumselenium-webdriverselenium-gridgeckodriver

Getting "The path to the driver executable must be set by the webdriver.gecko.driver " while try to run selenium Grid code with Gecko driver


I am running basic selenium code with the help of selenium Grid.

Below are the steps:

Step 1 :- Downloaded the latest version of selenium Standalone server (3.4.0);

Step 2 :- Created HUB by using command java - jar <path of selenium standalone server>\\selenium-server-standalone-3.4.0.jar -role hub -> run successfully;

Step 3 :- Created node using command java -jar selenium-server-standalone-3.4.0.jar -role hub -node http://localhost:4444/grid/register -> run successfully;

Step 4 :- Created a simple selenium program with below code:

public class ClassName {

  public static void main(String args[]) throws InterruptedException, MalformedURLException {
    System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
    DesiredCapabilities cap=DesiredCapabilities.firefox();
    cap.setPlatform(Platform.WINDOWS);
    cap.setBrowserName("firefox");
    URL url = new URL("http://localhost:4444/wd/hub");

    WebDriver wd1 = new RemoteWebDriver(url, cap);
    wd1.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    wd1.manage().window().maximize();

    wd1.get("http://www.clickindia.com/");
    wd1.findElement(By.linkText("Post Free Ad")).click();
    Thread.sleep(3000);
    wd1.findElement(By.linkText("Select category manually")).click();
    Thread.sleep(3000);
    WebElement country = wd1.findElement(By.id("combo_0"));
    Select sel = new Select(country);
    sel.selectByVisibleText("Jobs");
  }
}

While running above code the following exception was thrown:

Exception Image

Note : If I run above code without remoteDriver and as a common WebDriver programm then it is working and running properly.

Location of selenium standalone server and Gecko file is same.

Gecko version is v0.16.0

Thanks in advance


Solution

  • The error message indicates that Selenium cannot find GeckoDriver binary in:

    1. Any of the locations that are part of the PATH environment variable and
    2. It could not find any valid value via the JVM argument (System.getProperty("webdriver.gecko.driver")) which represents geckodriver binary's location .

    The line

    System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
    

    ensures that only the current JVM (The JVM in which your ClassName.main() method is executing) knows of the location of the geckodriver binary. That is why your code runs fine when you work with FirefoxDriver.

    But when you are working with RemoteWebDriver i.e., in the Grid mode, wherein you are trying to run against a Grid setup, setting the geckodriver location via the JVM argument "webdriver.gecko.driver" will not have any effect on the other JVMs (remember that the node which is responsible for supporting your browser interaction is spun off under a separate JVM using the command java -jar selenium-server-standalone-3.4.0.jar -role hub -node http://localhost:4444/grid/register (Your step 3).

    To fix this, you have two options.

    1. You use the JVM argument -Dwebdriver.gecko.driver when you are spawning the node and specify the location of the geckodriver.
    2. You download geckodriver binary into a folder and include its location as part of your PATH variable (i.e., add C:\\geckodriver.exe to your PATH variable)