Selenium Java - java.lang.IllegalStateException:驱动程序可执行文件的路径必须由webdriver.gecko.driver系统属性设置

时间:2016-09-27 10:35:21

标签: java selenium firefox junit

我的FireFox版本49.0.1 Selenium版本:Selenium-java-3.0.0-beta3 Java:8.0.1010.13 我用新文件替换了所有现有的Selenium Jar文件。添加gecko.Driver到我的代码仍然看到这条消息:

错误讯息: java.lang.IllegalStateException:驱动程序可执行文件的路径必须由webdriver.gecko.driver系统属性设置;

我的代码:

import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class AbstractPage {
WebDriver Driver =new FirefoxDriver();

@Before
public void Homescreen() throws InterruptedException
{
    System.getProperty("Webdriver.gecko.driver", "C:/geckodriver.exe");
    System.setProperty("Webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");      
    Driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Driver.get("URL");
    Driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}

@After
public void TestComplete()  {
    Driver.close();
}

@Test
public void Projects()  {
    Driver.findElement(By.id("login-form-username")).sendKeys("Login");
    Driver.findElement(By.id("login-form-password")).sendKeys("Password");
    Driver.findElement(By.id("quickSearchInput")).sendKeys("ID");

        }

}

3 个答案:

答案 0 :(得分:0)

您可以从代码中删除main()方法。解压缩你的gecko驱动程序并使用wires.exe将其保存到本地系统。

我的示例类路径是

G:\ ravik \ Ravi-Training \ Selenium \ Marionette for firefox \ wires.exe

public class AbstractPage 
{
WebDriver Driver;
System.setProperty("WebDriver.gecko.Driver", "C:\\TEMP\\Temp1_geckodriver-v0.10.0-win64.zip");
 Driver=new FirefoxDriver();
@Before
public void Homescreen() throws InterruptedException
{

    Driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Driver.get("https://QualityAssurance.com");
    Driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Driver.findElement(By.id("login-form-username")).sendKeys("Login");
    Driver.findElement(By.id("login-form-password")).sendKeys("Password");
    //JavascriptExecutor js = (JavascriptExecutor) Driver;
    //js.executeScript("document.getElementById('login-form-password').setAttribute('value', val );");
}

@After
public void TestComplete()  {
    Driver.close();
}

@Test
public void Projects()  {
    Driver.findElement(By.id("quickSearchInput")).sendKeys("WMSSE-229");

答案 1 :(得分:0)

请替换以下行

System.setProperty("WebDriver.gecko.Driver", C:\\TEMP\\Temp1_geckodriver-v0.10.0-win64.zip");

您应该传递geckodriver.exe而不是Zip文件。

String driverPath = "F:/Sample/Selenium3Example/Resources/";
System.setProperty("webdriver.firefox.marionette", driverPath+"geckodriver.exe");

在此处发布时,您可以使代码更清晰。

答案 2 :(得分:0)

你必须这样做:

System.setProperty("webdriver.gecko.driver", "C:/geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");

这是一个有效的示例,它让您了解上述代码段使用的上下文

package selenium;

import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Junit4FirefoxJava {

    private WebDriver driver;
    private String baseUrl;
    private boolean acceptNextAlert = true;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        System.setProperty("webdriver.gecko.driver", "C:/geckodriver.exe");
        System.setProperty("webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
        driver = new FirefoxDriver();
        baseUrl = "http://www.bing.com/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testJunit4IeJava() throws Exception {
        driver.get(baseUrl + "/");
        driver.findElement(By.id("sb_form_q")).click();
        driver.findElement(By.id("sb_form_q")).clear();
        driver.findElement(By.id("sb_form_q")).sendKeys("NTT data");
        driver.findElement(By.id("sb_form_go")).click();
        driver.findElement(By.linkText("NTT DATA - Official Site")).click();
        driver.findElement(By.id("js-arealanguage-trigger")).click();
        driver.findElement(By.linkText("Vietnam - English")).click();
        driver.findElement(By.id("MF_form_phrase")).clear();
        driver.findElement(By.id("MF_form_phrase")).sendKeys("internet");
        driver.findElement(By.cssSelector("input.search-button")).click();
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    private boolean isAlertPresent() {
        try {
            driver.switchTo().alert();
            return true;
        } catch (NoAlertPresentException e) {
            return false;
        }
    }

    private String closeAlertAndGetItsText() {
        try {
            Alert alert = driver.switchTo().alert();
            String alertText = alert.getText();
            if (acceptNextAlert) {
                alert.accept();
            } else {
                alert.dismiss();
            }
            return alertText;
        } finally {
            acceptNextAlert = true;
        }
    }

}