How to change the browser default download directory

Browsers have a default directory where downloaded files are stored. When your automation includes steps where the web browser initiates the download of files (for example clicking on a button), you will probably want to specify your own target directory for downloads. This allows the robot to reach the downloaded files and use them in the next steps of the process.

The following robot downloads a file to a custom browser download directory:

*** Settings ***
Documentation     Download a file to a custom browser download directory.
Library           RPA.Browser
Library           OperatingSystem

*** Variables ***
${FILENAME}=      file-sample_150kB.pdf
${DOWNLOAD_DIR}=    ${CURDIR}

*** Keywords ***
Wait For Download To Complete
    Wait Until Keyword Succeeds
    ...    2 min
    ...    5 sec
    ...    File Should Exist
    ...    ${FILENAME}

*** Tasks ***
Download a file to a custom browser download directory
    Set Download Directory     ${DOWNLOAD_DIR}
    Open Available Browser
    ...    https://file-examples.com/index.php/sample-documents-download/sample-pdf-download/
    Click Link    css:.download-button
    Wait For Download To Complete
    [Teardown]    Close All Browsers

Before opening the browser using the Open Available Browser keyword, we add the Set Download Directory keyword, passing it the directory where we want the files to be saved. In this case, we want the robot to download to the current directory (${CURDIR}).

The Set Download Directory keyword was added in the 6.4.0 version of the rpaframework, so make sure you are using version 6.4.0 or newer.