Reputation: 56
I am trying to bypass select printer and save as dialog using firefox profile in Selenium using the code given below. I want to save pdf file by clicking on print button to desired location without select printer and save as dialog.
I do no want to use Robot or Action class or AutoIT.
FirefoxProfile profile = new FirefoxProfile();
FirefoxOptions options = new FirefoxOptions();
System.setProperty("webdriver.gecko.driver", "drivers\\geckodriver.exe");
String downloadPath = "C:/downloads";
profile.setPreference("print.always_print_silent", true);
profile.setPreference("browser.download.folderList",1);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.download.dir",downloadPath);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf");
profile.setPreference("pdfjs.disabled", true);
WebDriver driver = new FirefoxDriver(options.setProfile(profile));
driver.get("google.com");
((JavascriptExecutor)driver).executeScript("window.print();");
By this, select printer is disabled but still it opens Save as window dialog.
Is there any way to disable both at the same time and we can save file at custom location without prompting window dialog?
Upvotes: 1
Views: 887
Reputation: 11
Gecko driver does not use profile preferences. instead you should create an FireFoxOptions object and add your preferences to it, then pass it to the driver.
FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.dir", DOWNLOAD_PATH);
options.addPreference("browser.download.folderList", 2);
options.addPreference("browser.download.manager.showWhenStarting", false);
options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream;application/xlsx");
WebDriver driver = new FirefoxDriver(options);
Upvotes: 1