且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Java中将Firefox配置文件和Firefox选项与Selenium一起使用

更新时间:2022-12-06 13:38:42

要使用新的

To use a new Firefox Profile through FirefoxOptions you can use the following code block:

System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(new FirefoxProfile());
WebDriver driver = new FirefoxDriver(options);
driver.get("https://www.google.com");

要使用现有的

To use an existing Firefox Profile through FirefoxOptions you can use the following code block:

System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
FirefoxOptions opt = new FirefoxOptions();
opt.setProfile(testprofile);
WebDriver driver =  new FirefoxDriver(opt);
driver.get("https://www.google.com");

要使用新的 Firefox配置文件通过 FirefoxOptions 以及

To use an new Firefox Profile through FirefoxOptions along with preferences you can use the following code block:

String downloadFilepath = "C:\\path\\to\\MozillaFirefoxDownload";
System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir",downloadFilepath);
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true);
options.setProfile(profile);
WebDriver driver =  new FirefoxDriver(options);
driver.get("https://www.google.com");


参考文献

您可以在以下位置找到几个相关的详细讨论:


References

You can find a couple of relevant detailed discussions in: