Set the Download Directory and Suppress Prompts
Configure Chrome, Firefox and Edge to save downloads silently into a directory the test controls, including PDFs that would otherwise open inline.
Stable Verified against Selenium 4.48.0
Problem
Downloads go to the user’s default folder, PDFs open in the browser’s viewer instead of downloading, and Firefox asks what to do with the file.
Solution
Set the browser preferences at driver creation. Chrome and Edge share the same preference keys; Firefox uses about:config preferences.
Download preferences per browser
Selenium 4 Stable
Path dir = Files.createTempDirectory("downloads");
// Chrome and EdgeChromeOptions chrome = new ChromeOptions();chrome.setExperimentalOption("prefs", Map.of( "download.default_directory", dir.toString(), "download.prompt_for_download", false, "download.directory_upgrade", true, "plugins.always_open_pdf_externally", true, // download PDFs instead of viewing "safebrowsing.enabled", true));
// FirefoxFirefoxOptions firefox = new FirefoxOptions();firefox.addPreference("browser.download.folderList", 2); // 2 = custom dirfirefox.addPreference("browser.download.dir", dir.toString());firefox.addPreference("browser.download.useDownloadDir", true);firefox.addPreference("browser.download.manager.showWhenStarting", false);firefox.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,text/csv,application/vnd.ms-excel,application/octet-stream,application/zip");firefox.addPreference("pdfjs.disabled", true); // no inline PDF viewerimport tempfile
directory = tempfile.mkdtemp("downloads")
# Chrome and Edgechrome = webdriver.ChromeOptions()chrome.add_experimental_option("prefs", { "download.default_directory": directory, "download.prompt_for_download": False, "download.directory_upgrade": True, "plugins.always_open_pdf_externally": True, "safebrowsing.enabled": True,})
# Firefoxfirefox = webdriver.FirefoxOptions()firefox.set_preference("browser.download.folderList", 2)firefox.set_preference("browser.download.dir", directory)firefox.set_preference("browser.download.useDownloadDir", True)firefox.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,text/csv,application/vnd.ms-excel,application/octet-stream,application/zip")firefox.set_preference("pdfjs.disabled", True)const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'downloads-'));
// Chrome and Edgeconst chromeOpts = new chrome.Options().setUserPreferences({'download.default_directory': dir,'download.prompt_for_download': false,'download.directory_upgrade': true,'plugins.always_open_pdf_externally': true,'safebrowsing.enabled': true,});
// Firefoxconst firefoxOpts = new firefox.Options().setPreference('browser.download.folderList', 2).setPreference('browser.download.dir', dir).setPreference('browser.download.useDownloadDir', true).setPreference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf,text/csv,application/vnd.ms-excel,application/octet-stream,application/zip').setPreference('pdfjs.disabled', true);var dir = Directory.CreateTempSubdirectory("downloads").FullName;
// Chrome and Edgevar chrome = new ChromeOptions();chrome.AddUserProfilePreference("download.default_directory", dir);chrome.AddUserProfilePreference("download.prompt_for_download", false);chrome.AddUserProfilePreference("download.directory_upgrade", true);chrome.AddUserProfilePreference("plugins.always_open_pdf_externally", true);chrome.AddUserProfilePreference("safebrowsing.enabled", true);
// Firefoxvar firefox = new FirefoxOptions();firefox.SetPreference("browser.download.folderList", 2);firefox.SetPreference("browser.download.dir", dir);firefox.SetPreference("browser.download.useDownloadDir", true);firefox.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,text/csv,application/vnd.ms-excel,application/octet-stream,application/zip");firefox.SetPreference("pdfjs.disabled", true);Why It Works
These are the same settings a user changes in the browser’s preferences, applied to the fresh profile Selenium creates for the session. A temporary directory per session keeps tests isolated and makes cleanup a directory delete.
Gotchas
- Firefox’s
neverAsk.saveToDiskmatches the server’sContent-Typeexactly; a CSV served astext/plainstill prompts. Check the response header in DevTools and add it. - Windows paths in Chrome prefs need backslashes or forward slashes consistently;
Path.toString()handles it. - On a Grid the directory is on the node; prefer Selenium’s managed downloads there.
safebrowsing.enabled: trueprevents Chrome from blocking “uncommon” downloads in some configurations; keep it.