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

This robot is included in our downloadable example robots. You can also find the code at the example robots repository.

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

The RPA.Dialogs library was added in the 5.2.0 version of the rpaframework, so make sure you are using version 5.2.0 or newer.

Run this robot locally in Robocorp Lab

You can run this robot on your local machine using Robocorp Lab:

  1. Set up your development environment.
  2. Download the example robots.
  3. Open the dialogs-upload-file example.
  4. Open the tasks.robot file and run it.

Run this robot as an assistant in Robocorp App

Follow these instructions to upload the robot to Robocorp Cloud and set up Robocorp App to execute it locally as a Robot Assistant.

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
Library           RPA.Excel.Files

*** Keywords ***
Collect Excel File From User
    Create Form    Upload Excel File
    Add File Input    label=Upload the Excel file with sales data
    ...    name=fileupload
    ...    element_id=fileupload
    ...    filetypes=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
    ...    target_directory=${CURDIR}${/}output
    &{response}    Request Response
    [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-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
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 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
    Create Form    Upload Excel File
    Add File Input    label=Upload the Excel file with sales data
    ...    name=fileupload
    ...    element_id=fileupload
    ...    filetypes=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
    ...    target_directory=${CURDIR}${/}output
    &{response}    Request Response
    [Return]    ${response["fileupload"][0]}
  • We create a form using the Create Form keyword from the RPA.Dialogs library.
  • We add a new file input field to our form (Add File Input) setting the label from the form, the name and id of the element, we specify the MIME file types that the element should accept, and the directory where the uploaded files will be stored.

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

  • The Request Response keyword will show the form to the user, and will assign the values that the user will enter into the form into the dictionary variable &{response}, which will have an item in it with the name that we assigned to our form 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.

Search form

For more complex forms, you can also specify your form structure in JSON format. Read more on the documentation pages for the library.

Running robot

Here is an example of this robot running as an assistant in Robocorp Cloud:

Dialogs Google Images Search Robot running