构造函数 FirefoxDriver(FirefoxOptions) 未定义

The constructor FirefoxDriver(FirefoxOptions) is undefined

我正在使用 Selenium 3.5.3,下面是我的代码。

我正在尝试在构造函数中使用 Firefox 选项,如
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/firefox/FirefoxDriver.html#FirefoxDriver-org.openqa.selenium.firefox.FirefoxOptions-

FirefoxOptions options=new FirefoxOptions();
options.setProfile(profile);
driver =new FirefoxDriver(options);

我在实例化 Firefox 驱动程序时遇到错误:

The constructor FirefoxDriver(FirefoxOptions) is undefined

我该如何解决这个问题?

Firefox 版本 55.0.3 64 位 壁虎驱动程序 v0.18.0

试试这个代码:

import java.io.File;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;

public class FirefoxOptionsDemo {

public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "E:\software and tools\geckodriver.exe");
FirefoxProfile profile =new FirefoxProfile(new File("C:\Users\sys\AppData\Roaming\Mozilla\Firefox\Profiles\ejtrne37.QAProfile"));
FirefoxOptions option=new FirefoxOptions();
option.setProfile(profile);
// Initialize Firefox driver
WebDriver driver = new FirefoxDriver(option);
//Maximize browser window
driver.manage().window().maximize();
//Go to URL which you want to navigate
driver.get("http://www.google.com");
//Set  timeout  for 5 seconds so that the page may load properly within that time
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//close firefox browser
driver.close();
}
}

这是供您参考的图片:

问题是由于旧的selenium版本共存。 mvn clean install 解决了这个问题。

您的问题可能与不同的原因有关。

常见的原因是您的 firefox 浏览器未对齐版本and/orgecko 驱动程序and/orselenium 库 .

我解决了这个问题升级 gecko 驱动程序和 selenium 库到最新版本。

就我而言,mvn clean install 没有解决问题