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

前端 未结 6 1023
清酒与你
清酒与你 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:17

    The Selenium client bindings will try to locate the geckodriver executable from the system PATH. You will need to add the directory containing the executable to the system path.

    • On Unix systems you can do the following to append it to your system’s search path, if you’re using a bash-compatible shell:

      export PATH=$PATH:/path/to/geckodriver
      
    • On Windows you need to update the Path system variable to add the full directory path to the executable. The principle is the same as on Unix.

    All below configuration for launching latest firefox using any programming language binding is applicable for Selenium2 to enable Marionette explicitly. With Selenium 3.0 and later, you shouldn't need to do anything to use Marionette, as it's enabled by default.

    To use Marionette in your tests you will need to update your desired capabilities to use it.

    Java :

    As exception is clearly saying you need to download latest geckodriver.exe from here and set downloaded geckodriver.exe path where it's exists in your computer as system property with with variable webdriver.gecko.driver before initiating marionette driver and launching firefox as below :-

    //if you didn't update the Path system variable to add the full directory path to the executable as above mentioned then doing this directly through code
    System.setProperty("webdriver.gecko.driver", "path/to/geckodriver.exe");
    
    //Now you can Initialize marionette driver to launch firefox
    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    capabilities.setCapability("marionette", true);
    WebDriver driver = new MarionetteDriver(capabilities); 
    

    And for Selenium3 use as :-

    WebDriver driver = new FirefoxDriver();
    

    If you're still in trouble follow this link as well which would help you to solving your problem

    .NET :

    var driver = new FirefoxDriver(new FirefoxOptions());
    

    Python :

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    caps = DesiredCapabilities.FIREFOX
    
    # Tell the Python bindings to use Marionette.
    # This will not be necessary in the future,
    # when Selenium will auto-detect what remote end
    # it is talking to.
    caps["marionette"] = True
    
    # Path to Firefox DevEdition or Nightly.
    # Firefox 47 (stable) is currently not supported,
    # and may give you a suboptimal experience.
    #
    # On Mac OS you must point to the binary executable
    # inside the application package, such as
    # /Applications/FirefoxNightly.app/Contents/MacOS/firefox-bin
    caps["binary"] = "/usr/bin/firefox"
    
    driver = webdriver.Firefox(capabilities=caps)
    

    Ruby :

    # Selenium 3 uses Marionette by default when firefox is specified
    # Set Marionette in Selenium 2 by directly passing marionette: true
    # You might need to specify an alternate path for the desired version of Firefox
    
    Selenium::WebDriver::Firefox::Binary.path = "/path/to/firefox"
    driver = Selenium::WebDriver.for :firefox, marionette: true
    

    JavaScript (Node.js) :

    const webdriver = require('selenium-webdriver');
    const Capabilities = require('selenium-webdriver/lib/capabilities').Capabilities;
    
    var capabilities = Capabilities.firefox();
    
    // Tell the Node.js bindings to use Marionette.
    // This will not be necessary in the future,
    // when Selenium will auto-detect what remote end
    // it is talking to.
    capabilities.set('marionette', true);
    
    var driver = new webdriver.Builder().withCapabilities(capabilities).build();
    

    Using RemoteWebDriver

    If you want to use RemoteWebDriver in any language, this will allow you to use Marionette in Selenium Grid.

    Python:

    caps = DesiredCapabilities.FIREFOX
    
    # Tell the Python bindings to use Marionette.
    # This will not be necessary in the future,
    # when Selenium will auto-detect what remote end
    # it is talking to.
    caps["marionette"] = True
    
    driver = webdriver.Firefox(capabilities=caps)
    

    Ruby :

    # Selenium 3 uses Marionette by default when firefox is specified
    # Set Marionette in Selenium 2 by using the Capabilities class
    # You might need to specify an alternate path for the desired version of Firefox
    
    caps = Selenium::WebDriver::Remote::Capabilities.firefox marionette: true, firefox_binary: "/path/to/firefox"
    driver = Selenium::WebDriver.for :remote, desired_capabilities: caps
    

    Java :

    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    
    // Tell the Java bindings to use Marionette.
    // This will not be necessary in the future,
    // when Selenium will auto-detect what remote end
    // it is talking to.
    capabilities.setCapability("marionette", true);
    
    WebDriver driver = new RemoteWebDriver(capabilities); 
    

    .NET

    DesiredCapabilities capabilities = DesiredCapabilities.Firefox();
    
    // Tell the .NET bindings to use Marionette.
    // This will not be necessary in the future,
    // when Selenium will auto-detect what remote end
    // it is talking to.
    capabilities.SetCapability("marionette", true);
    
    var driver = new RemoteWebDriver(capabilities); 
    

    Note : Just like the other drivers available to Selenium from other browser vendors, Mozilla has released now an executable that will run alongside the browser. Follow this for more details.

    You can download latest geckodriver executable to support latest firefox from here

    0 讨论(0)
  • 2020-11-22 05:18

    Every Driver service in selenium calls the similar code(following is the firefox specific code) while creating the driver object

     @Override
     protected File findDefaultExecutable() {
          return findExecutable(
            "geckodriver", GECKO_DRIVER_EXE_PROPERTY,
            "https://github.com/mozilla/geckodriver",
            "https://github.com/mozilla/geckodriver/releases");
        }
    

    now for the driver that you want to use, you have to set the system property with the value of path to the driver executable.

    for firefox GECKO_DRIVER_EXE_PROPERTY = "webdriver.gecko.driver" and this can be set before creating the driver object as below

    System.setProperty("webdriver.gecko.driver", "./libs/geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    
    0 讨论(0)
  • 2020-11-22 05:18

    I use from selenium-java-3.141.59 in windows 10 and solved my problem with this code:

    System.setProperty("webdriver.gecko.driver", "C:\\gecko\\geckodriver.exe");
    System.setProperty("webdriver.firefox.bin","C:\\Program Files\\Mozilla Firefox\\firefox.exe");
    WebDriver driver = new FirefoxDriver();
    
    0 讨论(0)
  • 2020-11-22 05:19
    1. Download gecko driver from the seleniumhq website (Now it is on GitHub and you can download it from Here) .
      1. You will have a zip (or tar.gz) so extract it.
      2. After extraction you will have geckodriver.exe file (appropriate executable in linux).
      3. Create Folder in C: named SeleniumGecko (Or appropriate)
      4. Copy and Paste geckodriver.exe to SeleniumGecko
      5. Set the path for gecko driver as below

    .

    System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    
    0 讨论(0)
  • 2020-11-22 05:22

    Selenium WebDriver Java code:

    Download Gecko Driver from https://github.com/mozilla/geckodriver/releases based on your platform. Extract it in a location by your choice. Write the following code:

    System.setProperty("webdriver.gecko.driver", "D:/geckodriver-v0.16.1-win64/geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.lynda.com/Selenium-tutorials/Mastering-Selenium-Testing-Tools/521207-2.html");
    
    0 讨论(0)
  • 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();
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题