ChromeDriver无法导航到eclipse中的URL

时间:2016-03-30 18:22:45

标签: java selenium selenium-webdriver cucumber selenium-chromedriver

我是Cucumber的新手,使用ChromeDriver请求网址时出现以下错误:

  

java.lang.IllegalStateException:驱动程序可执行文件的路径   必须由webdriver.chrome.driver系统属性设置;更多   信息,请参阅http://code.google.com/p/selenium/wiki/ChromeDriver。   最新版本可以从中下载   http://chromedriver.storage.googleapis.com/index.html at   com.google.common.base.Preconditions.checkState(Preconditions.java:177)

我的代码:

package cucumber.features;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class AddToList {

    WebDriver driver = null;

    @Given("^I am on Todo site$")
    public void onSite() throws Throwable {
        driver = new ChromeDriver();
        driver.navigate().to("http://localhost");
        System.out.println("on todo site");

    }

    @When("^Enter a task in todo textbox$")
    public void enterTask() throws Throwable {
        driver = new ChromeDriver();
        driver.findElement(By.name("task")).sendKeys("Test Unit Using Cucumber");
        ;
        System.out.println("task entered");
    }

    @Then("^I click on add to todo$")
    public void clickAddToTodo() throws Throwable {
        driver = new ChromeDriver();
        driver.findElement(By.xpath("//input[@value='Add to Todo' and @type='button']"));
        System.out.println("add button clicked");

    }

}

2 个答案:

答案 0 :(得分:2)

使用selenium库时遇到了类似的问题。我在创建驱动程序之前找到了这一行。

System.setProperty("webdriver.chrome.driver", PATH_TO_CHROME_DRIVER);

Here is simple project that could help you.

答案 1 :(得分:0)

this.driver.get(URL);

另外,我不认为你的When和Then应该创建新的ChromeDrivers。只有给定。我使用setUp方法实例化它

@Before
public void setUp() {       
    System.setProperty("webdriver.chrome.driver", "..//..//files//drivers//chromedriver.exe");
    this.driver = new ChromeDriver();       
}