My environment:
Firefox version : 78.9.0esr (64 bits)
Web driver : geckodriver-v0.26.0-win64
OS: Windows 10 (64 bits)
Python version: 3.8 (64 bits)
Python Selenium package version : 3.141.0
My requirement is to Download a CSV file from a web page via Selenium (Python code) but without opening the Dialog box. I mean, if you click manually on the download button, what happens is that you will see a dialog box asking for the name (if you want to change the default name) and location to save the file. What I wish to do is to bypass the dialog box step so when Selenium clicks on the button, the specific file would be downoaded to some default directory that I have specified with its default name. After a lot of googling (including threads on stackoverflow.com) I understood that this can be achieved in the following way by creating a new profile for the Firefox instance that Selenium uses. So here is my code:
self.profile = webdriver.FirefoxProfile()
self.profile.set_preference("browser.preferences.instantApply", True)
self.profile.set_preference("browser.download.folderList", 2)
self.profile.set_preference("browser.download.dir", os.getcwd())
self.profile.set_preference(
"browser.helperApps.neverAsk.saveToDisk",
"text/csv;charset=ISO-8859-1"
)
self.profile.set_preference(
"browser.helperApps.neverAsk.openFile",
"text/csv;charset=ISO-8859-1"
)
self.profile.set_preference(
"browser.download.manager.showWhenStarting",
False
)
self.profile.set_preference(
"browser.download.manager.showAlertOnComplete",
False
)
self.profile.set_preference("browser.download.panel.shown", False)
self.profile.set_preference(
"browser.download.manager.focusWhenStarting",
False
)
self.profile.set_preference(
"browser.download.manager.useWindow",
False
)
self.profile.set_preference(
"browser.download.manager.closeWhenDone",
False
)
self.web_driver = webdriver.Firefox(firefox_profile=self.profile)
Now the problem with the above mentioned code is that it still opens the dialog box and the only part that works is the following:
self.profile.set_preference("browser.download.folderList", 2)
self.profile.set_preference("browser.download.dir", os.getcwd()
But apart from that the main problem persists, that is, each time the dialog box appears. Now I've been looking into many pages on the web (forums, tutorials, etc.) and when I compare their suggested codes, it seems to me that I have proceeded in the same way and I don't see what else in terms of command/option should/could be added in order to prevent opening the dialog box and instead download the CSV file automatically. I would appreciate if you could kindly make some clarification and indicate what's the problem with my code.
Ok, so after a lot of searching on the internet, finally I found the answer to my question in the parameters page of the firefox itself (about:config) : browser.download.useDownloadDir
Here is the new version of my code that works pretty well and does the job:
self.profile = webdriver.FirefoxProfile()
self.profile.set_preference("browser.download.folderList", 2)
self.profile.set_preference("browser.download.dir", os.getcwd())
self.profile.set_preference(
"browser.helperApps.neverAsk.saveToDisk",
"text/csv"
)
self.profile.set_preference("browser.download.useDownloadDir", True)
self.web_driver = webdriver.Firefox(firefox_profile=self.profile)
So what has changed in this new version comapred to the previous version is:
I changed the MIME type to be simply text/csv
instead of
text/csv;charset=ISO-8859-1
I removed several profile parameters that seems to have no impact on the result.
And the most important part is that I added
browser.download.useDownloadDir
parameter to my profile. Based on different tests that I did, it seems that specifying the parameter
browser.helperApps.neverAsk.saveToDisk
alone is not enough. Indeed
once you define the desired download directory by using
browser.download.dir
then you really need to tell explicitly to
Firefox via the browser.download.useDownloadDir
parameter so that
browser uses automatically each time that directory while downloading
files.
I hope this might help those who may have encountered the same problem.