How to allow users to upload files for robots to use

Using the RPA.Dialogs library, you can add a user interface to your robots. One of the most useful features is the possibility for the user to upload a file, which is then used by the robot in its tasks.

Example: Providing an Excel file with data to fill a form in a web application

Get the code and run this example in your favorite editor on our Portal!

When run, the robot will:

  • present a form to the user to upload an Excel file
  • open a web browser
  • log into a web application
  • fill a form in the application with the data extracted from the robot file
  • save a screenshot and exit

rpaframework version 10.3.0 or newer is required.

Run this robot as an assistant in Robocorp Assistant

Follow these instructions to upload the robot to Control Room and set up Robocorp Assistant to execute it on your local machine.

Robot script

*** Settings ***
Documentation     Example robot to illustrate how to upload a file using the RPA.Dialogs library.
...               Collects an Excel file from the user and uses it to fill in the form at the
...               RobotSpareBin Industries Inc. intranet.
Library           RPA.Dialogs
Library           RPA.Browser.Selenium
Library           RPA.Excel.Files

*** Keywords ***
Collect Excel File From User
    Add heading    Upload Excel File
    Add file input
    ...    label=Upload the Excel file with sales data
    ...    name=fileupload
    ...    file_type=Excel files (*.xls;*.xlsx)
    ...    destination=${CURDIR}${/}output
    ${response}=    Run dialog
    [Return]    ${response.fileupload}[0]

*** Keywords ***
Open The Intranet Website
    Open Available Browser    https://robotsparebinindustries.com/

*** Keywords ***
Log In
    Input Text    id:username    maria
    Input Password    id:password    thoushallnotpass
    Submit Form
    Wait Until Page Contains Element    id:sales-form

*** Keywords ***
Fill And Submit The Form For One Person
    [Arguments]    ${salesRep}
    Input Text    firstname    ${salesRep}[First Name]
    Input Text    lastname    ${salesRep}[Last Name]
    Input Text    salesresult    ${salesRep}[Sales]
    ${target_as_string}=    Convert To String    ${salesRep}[Sales Target]
    Select From List By Value    salestarget    ${target_as_string}
    Click Button    Submit

*** Keywords ***
Fill The Form Using The Data From An Excel File
    [Arguments]    ${excel_file_path}
    Open Workbook    ${excel_file_path}
    ${salesReps}=    Read Worksheet As Table    header=True
    Close Workbook
    FOR    ${salesRep}    IN    @{salesReps}
        Fill And Submit The Form For One Person    ${salesRep}
    END

*** Keywords ***
Collect The Results
    Screenshot    css:div.sales-summary    ${CURDIR}${/}output${/}sales_summary.png

*** Keywords ***
Log Out And Close The Browser
    Click Button    Log out
    Close Browser

*** Tasks ***
Fill Robot Sparebin Intranet Sales Data From Excel File Provided By User
    ${excel_file_path}=    Collect Excel File From User
    Open The Intranet Website
    Log In
    Fill The Form Using The Data From An Excel File    ${excel_file_path}
    Collect The Results
    [Teardown]    Log Out And Close The Browser

Robot script explained

This example builds on top of the RobotSpareBin Intranet Robot Complete example, which is extensively explained in our Software Robot Developer Beginners' course, so here we will concentrate only on the RPA.Dialogs specific part of the implementation.

Settings

*** Settings ***
Documentation     Example robot to illustrate how to upload a file using the RPA.Dialogs library.
...               Collects an Excel file from the user and uses it to fill in the form at the
...               RobotSpareBin Industries Inc. intranet.
Library           RPA.Dialogs
Library           RPA.Browser.Selenium
Library           RPA.Excel.Files

The *** Settings *** section provides short Documentation for the script and imports libraries (Library) that add new keywords for the robot to use. In this case, we will add the RPA.Browser.Selenium library to control the web browser, the RPA.Dialogs library to build the UI for the user, and the RPA.Excel.Files to read the uploaded Excel file.

Tasks

*** Tasks ***
Fill Robot Sparebin Intranet Sales Data From Excel File Provided By User
    ${excel_file_path}=    Collect Excel File From User
    Open The Intranet Website
    Log In
    Fill The Form Using The Data From An Excel File    ${excel_file_path}
    Collect The Results
    [Teardown]    Log Out And Close The Browser
  • Fill Robot Sparebin Intranet Sales Data From Excel File Provided By User is the name of the robot's only task.
  • ${excel_file_path}= Collect Excel File From User: We are creating a variable to hold the path of the file that we collect from the user using the Collect Excel File From User keyword.
  • Open The Intranet Website: You guessed it. It opens the RobotSparebin Industries famous intranet website 🙂.
  • Log In logs the user in using the unsafely in-code stored credentials

    Friendly reminder: you should never use credentials directly in your code. See the Use a vault for secrets page for a better solution.

  • Fill The Form Using The Data From An Excel File ${excel_file_path} uses the uploaded Excel file to fill forms on the website.
  • Collect The First Search Result Image takes a screenshot once the operation is completed.

Keywords

*** Keywords ***
Collect Excel File From User
    Add heading    Upload Excel File
    Add file input
    ...    label=Upload the Excel file with sales data
    ...    name=fileupload
    ...    file_type=Excel files (*.xls;*.xlsx)
    ...    destination=${CURDIR}${/}output
    ${response}=    Run dialog
    [Return]    ${response.fileupload}[0]
  • The dialog is created automatically under the hood by the RPA.Dialogs library. You can start adding inputs without creating the dialog implicitly.
  • We set a heading for the dialog (Add heading).
  • We add a new file input field to our form (Add file input) setting the label, the name, the allowed file types that the input should accept, and the directory where the uploaded files will be stored.

In our case, we will accept only .xls and .xlsx files, and we want files to be stored in a folder called output in the root of our robot.

  • The Run dialog keyword allows us to show the dialog to the user, and to assign the values that the user will enter into the dialog into the result object ${response}, which will contain a property with the name that we assigned to our dialog field (fileupload).

The user can potentially upload more than one file to this field at a time. We are, however, interested only in the first one that is uploaded, so we return ${response.fileupload}[0] from our keyword.

File upload dialog

Learn more about the libraries mentioned on this page:

June 30, 2021