【问题标题】:Selenium Java - java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system propertySelenium Java - java.lang.IllegalStateException:驱动程序可执行文件的路径必须由 webdriver.gecko.driver 系统属性设置
【发布时间】:2017-02-04 22:52:40
【问题描述】:

我的 FireFox 版本 49.0.1 Selenium 版本:Selenium-java-3.0.0-beta3 爪哇: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");

        }

}

【问题讨论】:

  • 解压Gecko驱动并给出system.property的完整路径

标签: java selenium firefox junit


【解决方案1】:

您可以从代码中删除 main() 方法。解压缩您的壁虎驱动程序并使用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");

【讨论】:

    【解决方案2】:

    请替换下一行

    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");
    

    在此处发布时,您可以使您的代码更简洁。

    【讨论】:

      【解决方案3】:

      你必须这样做:

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

      这是一个工作示例,它让你知道上面的代码 sn-p 使用的上下文

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

      【讨论】:

        猜你喜欢
        • 2017-12-21
        • 2017-09-07
        • 2016-12-05
        • 2017-09-11
        • 1970-01-01
        • 2020-03-14
        • 1970-01-01
        • 2017-03-16
        • 1970-01-01
        相关资源
        最近更新 更多