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 14.1.1 or newer is recommended.

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       Insert the sales data for the week and export it as a PDF.
...                 Collects the input Excel file from the user.

Library             RPA.Browser.Selenium    auto_close=${FALSE}
Library             RPA.Dialogs
Library             RPA.Excel.Files
Library             RPA.HTTP
Library             RPA.PDF


*** Tasks ***
Insert the sales data for the week and export it as a PDF
    ${excel_file_path}=    Collect Excel file from the user
    Open the intranet website
    Log in
    Fill the form using the data from the Excel file    ${excel_file_path}
    Collect the results
    Export the table as a PDF
    [Teardown]    Log out and close the browser


*** Keywords ***
Collect Excel file from the 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=${OUTPUT_DIR}
    ${response}=    Run dialog
    RETURN    ${response.fileupload}[0]

Open the intranet website
    Open Available Browser    https://robotsparebinindustries.com/

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

Fill and submit the form for one person
    [Arguments]    ${sales_rep}
    Input Text    firstname    ${sales_rep}[First Name]
    Input Text    lastname    ${sales_rep}[Last Name]
    Input Text    salesresult    ${sales_rep}[Sales]
    Select From List By Value    salestarget    ${sales_rep}[Sales Target]
    Click Button    Submit

Fill the form using the data from the Excel file
    [Arguments]    ${excel_file_path}
    Open Workbook    ${excel_file_path}
    ${sales_reps}=    Read Worksheet As Table    header=True
    Close Workbook
    FOR    ${sales_rep}    IN    @{sales_reps}
        Fill and submit the form for one person    ${sales_rep}
    END

Collect the results
    Screenshot    css:div.sales-summary    ${OUTPUT_DIR}${/}sales_summary.png

Export the table as a PDF
    Wait Until Element Is Visible    id:sales-results
    ${sales_results_html}=    Get Element Attribute    id:sales-results    outerHTML
    Html To Pdf    ${sales_results_html}    ${OUTPUT_DIR}${/}sales_results.pdf

Log out and close the browser
    Click Button    Log out
    Close 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       Insert the sales data for the week and export it as a PDF.
...                 Collects the input Excel file from the user.

Library             RPA.Browser.Selenium    auto_close=${FALSE}
Library             RPA.Dialogs
Library             RPA.Excel.Files
Library             RPA.HTTP
Library             RPA.PDF

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 ***
Insert the sales data for the week and export it as a PDF
    ${excel_file_path}=    Collect Excel file from the user
    Open the intranet website
    Log in
    Fill the form using the data from the Excel file    ${excel_file_path}
    Collect the results
    Export the table as a PDF
    [Teardown]    Log out and close the browser
  • Insert the sales data for the week and export it as a PDF is the name of the robot's only task.
  • ${excel_file_path}= Collect Excel file from the 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 the 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 the Excel file ${excel_file_path} uses the uploaded Excel file to fill forms on the website.
  • Collect the results takes a screenshot once the operation is completed.

Keywords

*** Keywords ***
Collect Excel file from the 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=${OUTPUT_DIR}
    ${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

May 17, 2022