【发布时间】:2017-09-11 02:19:46
【问题描述】:
我正在使用 Selenium 3.3.1,我正在测试下面的代码。
运行后显示如下错误:
线程“主”java.lang.IllegalStateException 中的异常:路径 驱动程序可执行文件必须由 webdriver.gecko.driver 设置 系统属性;有关详细信息,请参阅 https://github.com/mozilla/geckodriver。最新版本可以 从https://github.com/mozilla/geckodriver/releases下载 com.google.common.base.Preconditions.checkState(Preconditions.java:738) 在 org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:111) 在 org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:38) 在 org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:112) 在 org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:302) 在 org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:233) 在 org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:125) 在 org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:121) 在 Selenium_login.(Selenium_login.java:13) 在 Selenium_login.main(Selenium_login.java:70) /home/ali/.cache/netbeans/dev/executor-sn-ps/run.xml:53:Java 返回:1 BUILD FAILED(总时间:0 秒)
Java 代码:
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();
}
}
【问题讨论】: