无法在Java中使用geckodriver启动FireFox自定义配置文件

时间:2018-08-03 06:47:32

标签: java firefox geckodriver

我正在尝试使用selenium v​​3.12和gecko-driver v2.10和Firefox 60.0版本启动包含附件的Firefox配置文件,但是自定义配置文件似乎无法正常运行。下面是我的代码

static WebDriver driver;
ProfilesIni profile = new ProfilesIni();
        myprofile = profile.getProfile("AutoProfile");
System.setProperty("webdriver.gecko.driver", 
  "E:\\Library\\geckodriver-v0.21.0-win32\\geckodriver.exe");
        driver = new FirefoxDriver(myprofile);

人为错误在线上

driver = new FirefoxDriver(myprofile);

  

构造函数FirefoxDriver(FirefoxProfile)未定义

1 个答案:

答案 0 :(得分:1)

您必须通过firefox选项传递它。

System.setProperty("webdriver.gecko.driver", "E:\\Library\\geckodriver-v0.21.0-win32\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("AutoProfile");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(myprofile);
WebDriver driver = new FirefoxDriver(firefoxOptions);

如果以下解决方案导致Java堆错误,则可以尝试DesiredCapabilities,如下所示:

System.setProperty("webdriver.gecko.driver","E:\\Library\\geckodriver-v0.21.0-win32\\geckodriver.exe");
File file = new File(path_to_your_firefox_profile);
DesiredCapabilities dc = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile(file);
dc.setCapability(FirefoxDriver.PROFILE, profile);
FirefoxDriver driver = new FirefoxDriver(dc);