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.