gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:54:30 24 4
gpt4 key购买 nike

我正在使用 Selenium 3.3.1我正在测试下面的代码。

运行后出现如下错误:

Exception in thread "main" java.lang.IllegalStateException: The pathto the driver executable must be set by the webdriver.gecko.driversystem property; for more information, seehttps://github.com/mozilla/geckodriver. The latest version can bedownloaded from https://github.com/mozilla/geckodriver/releases atcom.google.common.base.Preconditions.checkState(Preconditions.java:738)atorg.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:111)atorg.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:38)atorg.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:112)atorg.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:302)atorg.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:233)atorg.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:125)atorg.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:121)at Selenium_login.(Selenium_login.java:13) atSelenium_login.main(Selenium_login.java:70)/home/ali/.cache/netbeans/dev/executor-snippets/run.xml:53: Javareturned: 1 BUILD FAILED (total time: 0 seconds)

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();
}
}

最佳答案

从 Selenium 3.0 开始,您需要使用 geckodriver 与 Firefox 交互。从 github 下载geckodriver根据您的操作系统并将 geckodriver.exe 解压到文件夹中。

在初始化WebDriver之前添加以下行:

System.setProperty("webdriver.gecko.driver","c:/your/path/to/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
...

关于java - 驱动程序可执行文件的路径必须由 webdriver.gecko.driver 系统属性设置;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43418035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com