I am currently able to reach a web page and click on a "Download" button. When I do this, the download is launch in the default directory (Download... obviously)
I would like to create a folder on my desktop and download every file by default inside. How can I do? What do I need?
You have to overwrite the default download directory for your browser. This can be done via configuring the web driver. The Open Browser keyword of SeleniumLibrary provides separate parameters for this purpose to both Firefox (ff_profile_dir
) and to Chrome (options
).
Additionally you have to enable auto save and other browser configs depending on the specific browser you use.
Note that when working on Windows the path passed needs special attention:
Please note that if options string contains backslash, example a Windows OS path, the backslash needs escaping both in Robot Framework data and in Python side. This means single backslash must be writen using four backslash characters. Example, Windows path:
"C:\path\to\profile"
must be written as"C:\\\\path\\\to\\\\profile"
. Another way to write backslash is use Python raw strings and example write:r"C:\\path\\to\\profile"
.
Here is an example that will download the source of Robot Framework from GitHub using Firefox and SeleniumLibrary 4.3.0.
*** Settings ***
Library SeleniumLibrary
*** Test Case ***
Download Robot Framework into predefined folder
Open Browser https://github.com/robotframework/robotframework Firefox
... ff_profile_dir=set_preference("browser.download.folderList", 2);set_preference("browser.download.dir", r"C:\\Users\\myuser\\Documents\\Robotframework\\SO_material");set_preference("browser.helperApps.neverAsk.saveToDisk", "application/zip")
Click Element //summary[contains(.,'Code')]
Click Element //a[contains(.,'Download ZIP')]
[Teardown] Close Browser
The browser parameters set:
browser.download.folderList
was set to 2
where
2
indicates a custom (see: browser.download.dir) folderSource: Download Manager preferences
browser.download.dir
is the path to the custom folder. Make sure to pass the path as described in the documentation. I have used Python raw string in this example.
browser.helperApps.neverAsk.saveToDisk
is application/zip
which means that any downloaded zip will be saved without prompting for download location.
You can find links to lists of Chrome and Firefox arguments in this Stack Overflow answer: List of Firefox and Chrome arguments/preferences.