即使在代码中定义,也会出现 Geckodriver 错误

Getting Geckodriver error evenif it is defined in code

我正在为我的基本 Maven 和 Selenium 项目 openGmail 使用 2 classes。我将 Selenium 3.5 与 Firefox 47.0.1 和 Gecodriver 0.18 一起使用。

我的主要 class 是:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MainClass {

    WebDriver driver = new FirefoxDriver();

    public void setup() {
        String Path_GecoDriver="C:/Personal/Selenium/setup/geckodriver-v0.18.0-win64";
        System.setProperty("webdriver.firefox.marionette", Path_GecoDriver+"/geckodriver.exe");
        System.setProperty("webdriver.gecko.driver", Path_GecoDriver+"/geckodriver.exe");

            }
    public void OpenBrowser() {
        String url="http://google.co.in";
        driver.get(url);
    }
    public void LoginGmail() throws InterruptedException {
        String username ="username";
        String passwd = "passwd";
        driver.findElement(By.linkText("Gmail")).click();
        driver.findElement(By.id("identifierId")).sendKeys(username);
        WebElement cli=driver.findElement(By.xpath("//*[text()='Next']"));
        cli.click();
        Thread.sleep(1000);
        driver.findElement(By.xpath("//input[@name='password']")).sendKeys(passwd);
        driver.findElement(By.xpath("//*[text()='Next']")).click();
    }
    public void CloseBrowser() {
                driver.close();
    }
}

我的另一个class是:-

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestngClass {

MainClass mc=new MainClass();

@Before
public void sp() {
mc.setup();
mc.OpenBrowser();
}

@Test
public void LG() throws InterruptedException {
            mc.LoginGmail();
}
@After
public void CB() {
    mc.CloseBrowser();
}
}

我遇到错误

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information

我试图通过将我的 System.setProperty (prop, path/to/driver) 放在我的@Before 中来执行测试,@test 也没有改变,但我仍然收到错误。

如果我使用单个 class,那么一切正常,所以我认为我将 system.setProperty 放在了错误的位置。

我最近才开始使用 Java 和 Selenium。我什至尝试将我的 Gecko 驱动程序 exe 放入我的 Maven 项目的 src/main/resources 中,如 In System.setProperty("webdriver.gecko.driver", "<Path to your WebDriver>"), what is meant by "Path to your WebDriver"?

中所述

您已经设置了两次 属性 如下:-

  System.setProperty("webdriver.firefox.marionette", Path_GecoDriver+"/geckodriver.exe");
  System.setProperty("webdriver.gecko.driver", Path_GecoDriver+"/geckodriver.exe");

删除:-

System.setProperty("webdriver.firefox.marionette", Path_GecoDriver+"/geckodriver.exe");

另一件事是你必须在创建 firefox 实例之前设置它。您首先定义了实例,然后设置首选项。

试试下面的方法:-

 WebDriver driver =null;

  public void setup() {
      String Path_GecoDriver="C:/Personal/Selenium/setup/geckodriver-v0.18.0-win64";
      System.setProperty("webdriver.gecko.driver", Path_GecoDriver+"/geckodriver.exe");
      driver= new FirefoxDriver();

}

希望对您有所帮助:)