当前位置: 首页 > 知识库问答 >
问题:

webdriver必须设置驱动程序可执行文件的路径。壁虎。驱动系统属性;

巫欣荣
2023-03-14

我正在使用Selenium3.3.1,我正在测试下面的代码。

运行后,将显示以下错误:

线程“main”java中出现异常。lang.IllegalStateException:驱动程序可执行文件的路径必须由webdriver设置。壁虎。驱动系统属性;有关详细信息,请参阅https://github.com/mozilla/geckodriver.最新版本可从以下网站下载:https://github.com/mozilla/geckodriver/releases在com.google.常见的基础先决条件。org上的checkState(premissions.java:738)。openqa。硒。遥远的服务司机服务。findExecutable(DriverService.java:111)位于org。openqa。硒。火狐。壁虎服务。在org上访问$100(GeckoDriverService.java:38)。openqa。硒。火狐。GeckoDriverService$Builder。findDefaultExecutable(GeckoDriverService.java:112)位于org。openqa。硒。遥远的服务DriverService$Builder。在org上构建(DriverService.java:302)。openqa。硒。火狐。火狐司机。toExecutor(FirefoxDriver.java:233)位于org。openqa。硒。火狐。火狐司机。(FirefoxDriver.java:125)位于org。openqa。硒。火狐。火狐司机。(FirefoxDriver.java:121)登录。(Selenium_login.java:13)在Selenium_登录时。main(Selenium_login.java:70)/home/ali/。cache/netbeans/dev/executor snippets/run。xml:53:Java返回:1生成失败(总时间: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();
    }
}

共有2个答案

章睿
2023-03-14

例如,selenium中的驱动程序构造函数

WebDriver driver = new FirefoxDriver();

搜索驱动程序可执行文件,在本例中,firefox驱动程序搜索gecko驱动程序可执行文件。如果服务无法找到可执行文件,将引发异常

这就是异常的来源(注意checkstate方法)

 /**
   *
   * @param exeName Name of the executable file to look for in PATH
   * @param exeProperty Name of a system property that specifies the path to the executable file
   * @param exeDocs The link to the driver documentation page
   * @param exeDownload The link to the driver download page
   *
   * @return The driver executable as a {@link File} object
   * @throws IllegalStateException If the executable not found or cannot be executed
   */
  protected static File findExecutable(
      String exeName,
      String exeProperty,
      String exeDocs,
      String exeDownload) {
    String defaultPath = new ExecutableFinder().find(exeName);
    String exePath = System.getProperty(exeProperty, defaultPath);
    checkState(exePath != null,
        "The path to the driver executable must be set by the %s system property;"
            + " for more information, see %s. "
            + "The latest version can be downloaded from %s",
            exeProperty, exeDocs, exeDownload);

    File exe = new File(exePath);
    checkExecutable(exe);
    return exe;
  }

下面是抛出异常的check state方法

  /**
   * Ensures the truth of an expression involving the state of the calling instance, but not
   * involving any parameters to the calling method.
   *
   * <p>See {@link #checkState(boolean, String, Object...)} for details.
   */
  public static void checkState(
      boolean b,
      @Nullable String errorMessageTemplate,
      @Nullable Object p1,
      @Nullable Object p2,
      @Nullable Object p3) {
    if (!b) {
      throw new IllegalStateException(format(errorMessageTemplate, p1, p2, p3));
    }
  }

解决方案:在创建驱动程序对象之前设置系统属性,如下所示

System.setProperty("webdriver.gecko.driver", "./libs/geckodriver.exe");
WebDriver driver = new FirefoxDriver();

以下是驱动程序服务搜索驱动程序可执行文件的代码段(适用于firefox和chrome):

火狐:

@Override
 protected File findDefaultExecutable() {
      return findExecutable(
        "geckodriver", GECKO_DRIVER_EXE_PROPERTY,
        "https://github.com/mozilla/geckodriver",
        "https://github.com/mozilla/geckodriver/releases");
    }

Chrome:

   @Override
    protected File findDefaultExecutable() {
      return findExecutable("chromedriver", CHROME_DRIVER_EXE_PROPERTY,
          "https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver",
          "http://chromedriver.storage.googleapis.com/index.html");
    }

其中GECKO_DRIVER_EXE_PROPERTY=“webdriver.GECKO.DRIVER”和CHROME_DRIVER_EXE_PROPERTY=“webdriver.CHROME.DRIVER”

其他浏览器的情况也类似,下面是可用浏览器实现列表的快照

谢承颜
2023-03-14

自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();
...
 类似资料: