Logging out
Taking out the trash
The process is now automated! Well done, you!
Let's still do some housekeeping and clean up after ourselves. Maria told us that after completing her task, she logs out and closes her browser. This might not come as a huge surprise for you anymore, but our robot can do this thing, too!
Let's add the next (last!) step, log_out()
, to our task, as the browser is already automatically closed at the end of each run:
@task
def robot_spare_bin_python():
"""Insert the sales data for the week and export it as a PDF"""
browser.configure(
slowmo=100,
)
open_the_intranet_website()
log_in()
download_excel_file()
fill_form_with_excel_data()
collect_results()
log_out()
Then (you guessed it, right? 🙂) we add a new function:
def log_out():
"""Presses the 'Log out' button"""
You already know about the page.click()
function, so let's go with that for the logout:
from robocorp.tasks import task
from robocorp import browser
from RPA.HTTP import HTTP
from RPA.Excel.Files import Files
@task
def robot_spare_bin_python():
"""Insert the sales data for the week and export it as a PDF"""
browser.configure(
slowmo=100,
)
open_the_intranet_website()
log_in()
download_excel_file()
fill_form_with_excel_data()
collect_results()
log_out()
def open_the_intranet_website():
"""Navigates to the given URL"""
browser.goto("https://robotsparebinindustries.com/")
def log_in():
"""Fills in the login form and clicks the 'Log in' button"""
page = browser.page()
page.fill("#username", "maria")
page.fill("#password", "thoushallnotpass")
page.click("button:text('Log in')")
def fill_and_submit_sales_form(sales_rep):
"""Fills in the sales data and click the 'Submit' button"""
page = browser.page()
page.fill("#firstname", sales_rep["First Name"])
page.fill("#lastname", sales_rep["Last Name"])
page.select_option("#salestarget", str(sales_rep["Sales Target"]))
page.fill("#salesresult", str(sales_rep["Sales"]))
page.click("text=Submit")
def download_excel_file():
"""Downloads excel file from the given URL"""
http = HTTP()
http.download(url="https://robotsparebinindustries.com/SalesData.xlsx", overwrite=True)
def fill_form_with_excel_data():
"""Read data from excel and fill in the sales form"""
excel = Files()
excel.open_workbook("SalesData.xlsx")
worksheet = excel.read_worksheet_as_table("data", header=True)
excel.close_workbook()
for row in worksheet:
fill_and_submit_sales_form(row)
def collect_results():
"""Take a screenshot of the page"""
page = browser.page()
page.screenshot(path="output/sales_summary.png")
def log_out():
"""Presses the 'Log out' button"""
page = browser.page()
page.click("text=Log out")
If we run it, we will see that it logs out and closes the browser at the end. Such a polite robot!