How to use both the Firefox Profile and Firefox options with Selenium in Java

被刻印的时光 ゝ 提交于 2021-02-10 19:59:44

问题


I'm writing a test that I'd like to headless, which will also download a file within java using Selenium. from here I learn that you can set a driver to be headless by throwing this code before you initialize the driver:

options.setHeadless(true); //sets driver to work headless 
WebDriver driver = new FirefoxDriver(options);

and that I can use this method to write a Firefox Profile which will dictate a download directory and allow me to download a file with firefox w/o any pop up windows (I've modified the method to allow the method to permit the download location as an argument). After creating the method, I call it within main like this:

downloadPath = "C:\Scripts"
WebDriver driver = new FirefoxDriver(FirefoxDriverProfile(downloadPath));

and then say I want to use the following code with either of the two methods above:

driver.get(https://github.com/mozilla/geckodriver/releases);
driver.findElement(By.linkText("geckodriver-v0.27.0-win64.zip")).click();

I'll either not have a headless version of firefox running, or I get a pop up save prompt when I go to download the zip file.

How can I combine these two funcitons, the profile and the options?

edit: fixed the setHeadless(false) to be setHedless(true)


回答1:


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");

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:

  • Cannot resolve constructor FirefoxDriver(org.openqa.selenium.firefox.FirefoxProfile)
  • unable to Pass FirefoxProfile parameter In webdriver to use preferences to download file



回答2:


The function options.setHeadless(false) should have a true parameter not false



来源:https://stackoverflow.com/questions/63477601/how-to-use-both-the-firefox-profile-and-firefox-options-with-selenium-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!