Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The path to the driver executable must be set by the webdriver.gecko.driver system property; [duplicate]

I am using Selenium 3.3.1 and I'm testing the code below.

After running the following error is displayed:

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:738) at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:111) at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:38) at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:112) at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:302) at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:233) at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:125) at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:121) at Selenium_login.(Selenium_login.java:13) at Selenium_login.main(Selenium_login.java:70) /home/ali/.cache/netbeans/dev/executor-snippets/run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

Java code:

import java.io.*;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_login {

    public WebDriver driver = new FirefoxDriver();

    /**
     * Open the test website.
     */
    public void openTestSite() {
        driver.navigate().to("http://testing-ground.scraping.pro/login");
    }

    /**
     * 
     * @param username
     * @param Password
     * 
     *            Logins into the website, by entering provided username and
     *            password
     */
    public void login(String username, String Password) {

        WebElement userName_editbox = driver.findElement(By.id("usr"));
        WebElement password_editbox = driver.findElement(By.id("pwd"));
        WebElement submit_button = driver.findElement(By.xpath("//input[@value='Login']"));

        userName_editbox.sendKeys(username);
        password_editbox.sendKeys(Password);
        submit_button.click();

    }

    /**
     * grabs the status text and saves that into status.txt file
     * 
     * @throws IOException
     */
    public void getText() throws IOException {
        String text = driver.findElement(By.xpath("//div[@id='case_login']/h3")).getText();
        Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("status.txt"), "utf-8"));
        writer.write(text);
        writer.close();

    }

    /**
     * Saves the screenshot
     * 
     * @throws IOException
     */
    public void saveScreenshot() throws IOException {
        File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("screenshot.png"));
    }

    public void closeBrowser() {
        driver.close();
    }

    public static void main(String[] args) throws IOException {
        Selenium_login webSrcaper = new Selenium_login();
        webSrcaper.openTestSite();
        webSrcaper.login("admin", "12345");
        webSrcaper.getText();
        webSrcaper.saveScreenshot();
        webSrcaper.closeBrowser();
    }
}
like image 875
Ali Hesari Avatar asked Oct 27 '25 08:10

Ali Hesari


1 Answers

You need to use geckodriver to interact with Firefox since Selenium 3.0. Download geckodriver from github depending upon your OS and extract geckodriver.exe into a folder.

Add the following line before initializing WebDriver:

System.setProperty("webdriver.gecko.driver","c:/your/path/to/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
...
like image 190
Kushal Bhalaik Avatar answered Oct 28 '25 23:10

Kushal Bhalaik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!