Webinar

November 3rd, 2021 12:00 PM EDT
Automation for Field Services & DistributionNovember 3rd, 2021 12:00 PM EDT
Learn how creating a digital workforce can improve your supply chain processes!

Bonus chapter: Python robot

The same robot in Pure Python and with Playwright instead of Selenium

The following chapter is totally optional but provides interesting information about alternative approaches!

In this course, we implemented the robot using Robot Framework. However, that is not your only option!

You can also implement your robots in pure Python!

Let's see how the same robot might look like in Python. For browser automation, let's use the Playwright-based Robot Framework Browser library instead of the Selenium-based RPA.Browser.Selenium library!

Configure the dependencies in conda.yaml

Let's use this conda.yaml file to define the dependencies and the initialization command we need to implement this robot (nodejs, rpaframework and robotframework-browser):

channels:
  - conda-forge
dependencies:
  - python=3.7.5
  - pip=20.1
  - nodejs=14.17.4
  - pip:
      - robotframework-browser==9.0.1
      - rpaframework==11.4.0
rccPostInstall:
  - rfbrowser init

Define the robot to run in robot.yaml

This robot.yaml configuration will handle executing the robot (task.py):

tasks:
  Default:
    command:
      - python
      - task.py

condaConfigFile: conda.yaml
artifactsDir: output
PATH:
  - .
PYTHONPATH:
  - .
ignoreFiles:
  - .gitignore

The command is simply python task.py. Read more about robot structure & configuration.

Implement the Python robot in task.py

Here's what the robot might look like in Python and using the Playwright-based Browser library for the browser automation:

# Robot to enter weekly sales data into the RobotSpareBin Industries Intranet.

import os

from Browser import Browser
from Browser.utils.data_types import SelectAttribute
from RPA.Excel.Files import Files
from RPA.FileSystem import FileSystem
from RPA.HTTP import HTTP
from RPA.PDF import PDF


browser = Browser()


def open_the_intranet_website():
    browser.open_browser("https://robotsparebinindustries.com/")


def log_in():
    browser.type_text("css=#username", "maria")
    browser.type_secret("css=#password", "thoushallnotpass")
    browser.click("text=Log in")


def download_the_excel_file():
    http = HTTP()
    http.download(
        url="https://robotsparebinindustries.com/SalesData.xlsx",
        overwrite=True)


def fill_and_submit_the_form_for_one_person(sales_rep):
    browser.type_text("css=#firstname", sales_rep["First Name"])
    browser.type_text("css=#lastname", sales_rep["Last Name"])
    browser.type_text("css=#salesresult", str(sales_rep["Sales"]))
    browser.select_options_by(
        "css=#salestarget",
        SelectAttribute["value"],
        str(sales_rep["Sales Target"]))
    browser.click("text=Submit")


def fill_the_form_using_the_data_from_the_excel_file():
    excel = Files()
    excel.open_workbook("SalesData.xlsx")
    sales_reps = excel.read_worksheet_as_table(header=True)
    excel.close_workbook()
    for sales_rep in sales_reps:
        fill_and_submit_the_form_for_one_person(sales_rep)


def collect_the_results():
    browser.take_screenshot(
        filename=f"{os.getcwd()}/output/sales_summary.png",
        selector="css=div.sales-summary")


def export_the_table_as_a_pdf():
    sales_results_html = browser.get_property(
        selector="css=#sales-results", property="outerHTML")
    pdf = PDF()
    pdf.html_to_pdf(sales_results_html, "output/sales_results.pdf")


def log_out():
    browser.click("text=Log out")


def main():
    try:
        open_the_intranet_website()
        log_in()
        download_the_excel_file()
        fill_the_form_using_the_data_from_the_excel_file()
        collect_the_results()
        export_the_table_as_a_pdf()
    finally:
        log_out()
        browser.playwright.close()


if __name__ == "__main__":
    main()

As you can see, instead of keywords, we use methods. The main method, as the name implies, acts as the main method that calls the other methods. The try and finally construct is used instead of the Teardown in Robot Framework. We use the same libraries as with the Robot Framework version, only this time in Python!

What we learned

  • Robots can also be implemented in pure Python!
  • There is more than one library for browser automation! In addition to Selenium, Robocorp supports Playwright.
  • Choosing between Robot Framework, Python, or a mixture of both is up to you. Some consider Robot Framework syntax easier to read and approach than Python. On the other hand, people already familiar with Python might prefer skipping the Robot Framework layer altogether!
  • Good to know: Robot Framework is built with Python, and the keywords end up eventually calling Python under the hood.