2

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)

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

3

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

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

  • oh shoot, thanks for catching that. that being said, I still can only either call the driver with either the options or the profile, I dont know how to call them both. – Jonathan-Zollinger Aug 18 '20 at 23:09