Selenium webdriver gecko error: The path to the driver must be set by webdriver.gecko

Selenium webdriver gecko error: The path to the driver must be set by webdriver.gecko

试图让 selenium 工作但没有工作,并显示以下错误。

Libs:junit4.12, selenium-java-3.4, selenium-server-standalone-3.5

任何人都可以看看并告诉我代码有什么问题或者是什么 不见了,请。 每次开始测试时,都会出现 2 个错误。 底部嵌入的屏幕截图显示了错误详细信息。

我的代码如下:

    package com.example.tests;

    import java.util.regex.Pattern;
    import java.util.concurrent.TimeUnit;
    import org.junit.*;
    import static org.junit.Assert.*;
    import static org.hamcrest.CoreMatchers.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.support.ui.Select;

    public class TestCase16 {
      private WebDriver driver;
      private String baseUrl;
      private boolean acceptNextAlert = true;
      private StringBuffer verificationErrors = new StringBuffer();

      @Before
      public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "URL";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

       System.setProperty("webdriver.gecko.driver", "C:\Users\ayre1de\Downloads\geckodriver-v0.18.0-win64\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("URL");

       }

      @Test
      public void testCaseAcandoIntranet() throws Exception {
        driver.get(baseUrl + "/");
        driver.findElement(By.id("IDToken1")).clear();
        driver.findElement(By.id("IDToken1")).sendKeys("XXX");
        driver.findElement(By.id("IDToken2")).clear();
        driver.findElement(By.id("IDToken2")).sendKeys("*XXX");
        driver.findElement(By.name("Login.Submit")).click();
        driver.findElement(By.id("yui_patched_v3_18_1_1_1502961326988_317")).click();
        driver.findElement(By.xpath("//a[@id='_com_liferay_product_navigation_user_personal_bar_web_portlet_ProductNavigationUserPersonalBarPortlet_sidenavUserToggle']/span/div")).click();
        driver.findElement(By.linkText("Abmelden")).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;
        }
      }
    }
    `

问题是您正在像下面这样初始化 webdriver 实例两次。特别是您错过了为 webdriver 的第一个实例指定 System.setProperty 的地方,而您的代码恰恰是失败的:-

    driver = new FirefoxDriver();  // 1st instance
    baseUrl = "URL";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    System.setProperty("webdriver.gecko.driver", "C:\Users\ayre1de\Downloads\geckodriver-v0.18.0-win64\geckodriver.exe");
    WebDriver driver = new FirefoxDriver(); // 2nd instance
    driver.get("URL");

现在删除以上两行并编写如下代码:-

    System.setProperty("webdriver.gecko.driver", "C:\Users\ayre1de\Downloads\geckodriver-v0.18.0-win64\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get("URL");