Selenium using Java - The path to the driver executable must be set by the webdriver.gecko.driver system property

前端 未结 6 1081
清酒与你
清酒与你 2020-11-22 05:01

I am trying to launch Mozilla but still I am getting this error:

Exception in thread \"main\" java.lang.IllegalStateException: The path to the driver

6条回答
  •  春和景丽
    2020-11-22 05:39

    in my case, I must to set path in properties file, in many hours I find the way:

    application.properties file:

    webdriver.gecko.driver="/lib/geckodriver-v0.26.0-win64/geckodriver.exe"
    

    in java code:

    private static final Logger log = Logger.getLogger(Login.class.getName());
    private FirefoxDriver driver;
    private FirefoxProfile firefoxProfile;
    private final String BASE_URL = "https://www.myweb.com/";
    private static final String RESOURCE_NAME = "main/resources/application.properties"; // could also be a constant
    private Properties properties;
    
    public Login() {
        init();
    }
    
    private void init() {
        properties = new Properties();
        try(InputStream resourceStream = getClass().getClassLoader().getResourceAsStream(RESOURCE_NAME)) {
            properties.load(resourceStream);
        } catch (IOException e) {
            System.err.println("Could not open Config file");
            log.log(Level.SEVERE, "Could not open Config file", e);
        }
        // open incognito tab by default
        firefoxProfile = new FirefoxProfile();
        firefoxProfile.setPreference("browser.privatebrowsing.autostart", true);
        // geckodriver driver path to run
        String gekoDriverPath = properties.getProperty("webdriver.gecko.driver");
        log.log(Level.INFO, gekoDriverPath);
        System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + gekoDriverPath);
        log.log(Level.INFO, System.getProperty("webdriver.gecko.driver"));
        System.setProperty("webdriver.gecko.driver", System.getProperty("webdriver.gecko.driver").replace("\"", ""));
        if (driver == null) {
            driver = new FirefoxDriver();
        }
    
    }
    

提交回复
热议问题