-3

I got this error when executing a script . I have upgrade FF(56), Gecko(v0.19) and Selenium 3.6...

This is my code:

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

public class Testing {
public static void main(String[] args) throws InterruptedException{
WebDriver  driver = new FirefoxDriver();
System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
driver.get("http://www.facebook.com");
Thread.sleep(5000);
System.out.println("website is opened");
driver.close();
}
}

Error in Logs:

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 https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases at com.google.common.base.Preconditions.checkState(Preconditions.java:754) at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124) at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:40) at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:114) at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:329) at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:150) at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:120) at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:98) at god.Testing.main(Testing.java:8)

iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
Nayan
  • 21
  • 1
  • 3

2 Answers2

0

Replace the position of setProperty() method, it should before get() method

Try this code:

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

public class Testing {
    public static void main(String[] args) throws InterruptedException{
    System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
    WebDriver  driver = new FirefoxDriver();
    driver.get("http://www.facebook.com");
    Thread.sleep(5000);
    System.out.println("website is opened");
    driver.close();
    }

}
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
0

The error we are seeing on the Console as IllegalStateException is pretty correct and justified.

Explaination:

In your code block, right in the begining, you have first initialized the WebDriver instance and casted it to FirefoxDriver as follows:

WebDriver  driver = new FirefoxDriver();

When this line of code is executed, Java Compiler have no information about your Test Configuration of using geckodriver.exe as the line for Test Configuration is mentioned later in your program as follows:

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

Hence, in absence of finding a proper driver executable, Java Compiler complains of java.lang.IllegalStateException.

Solution:

The solution to your issue would be to rearrange the 2 lines of code as follows:

System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
WebDriver  driver = new FirefoxDriver();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352