Logging into the intranet
Now that it has opened the browser, our robot needs to log into the intranet, using Maria's credentials.
The second step: logging in
Let's add the next logical step to our task. We'll call it Log In
. The *** Tasks ***
cell should look like this after adding the step:
*** Tasks ***
Insert the sales data for the week and export it as a PDF
Open The Intranet Website
Log In
Add a new *** Keywords ***
cell before the *** Tasks ***
cell and add this code to it:
*** Keywords ***
Log In
Ok! Now we have to tell our robot to find the correct fields on the page and fill them.
Note: Defining each keyword in a separate Notebook cell makes it possible to execute them individually. This is useful in more complex projects, where you might want to run and edit a single keyword instead of running the full robot all the time. You could add the new
Log In
keyword to the existingOpen The Intranet Website
keyword cell, too, but then you would lose the ability to execute the keywords individually.
Finding a form field
You, a human being (just assuming!), can see and find things such as forms and fields on a web page. Your robot, however, lacks both vision and the ability to think. It needs very specific instructions to find anything.
A web page is written in HTML (Hypertext Markup Language). It defines the meaning and structure of web content. The RobotSpareBin intranet is also built using HTML.
To find forms, fields, and other elements from an HTML web page, your robot needs to know their locators. Think of a locator as the street address for an element, such as the username field on the RobotSpareBin intranet login page.
If you want, you can go deeper and learn more about how to find interface elements in web applications and the concept of locators in this dedicated article.
This is what the username field HTML markup looks like:
<input type="text" id="username" name="username" required="" class="form-control"/>
It is an input
element. It has several attributes
, such as type
, id
and name
. We can use these to locate (find) the elements.
We need to tell the robot to fill in the login form. The RPA.Browser
library provides a Input Text
keyword that can fill in text fields. The keyword takes two arguments: The locator
argument tells where to input. The value
argument tells what to input.
In our case, we can call the keyword like this to input maria
in the field that has an attribute name
with the value of username
:
Input Text username maria
Note that you need to have at least two spaces between the keyword and each of the arguments.
Filling and submitting the form
Modify the Log in
*** Keywords ***
cell, so that it looks like this:
*** Keywords ***
Log In
Input Text username maria
Now run the robot, and it will fill the username field:
Great! Now we can do the same thing for the password: the only difference is that we should use the more specific Input Password
keyword. This keyword accepts the same arguments, with the difference that it will not output the value of our secret password in the log.
Note: In a real project, you should take measures to never commit your credentials in your robot code. To learn more about how to secure your sensitive data in Robocorp Cloud, you can read this article.
So our keyword now is:
*** Keywords ***
Log In
Input Text username maria
Input Password password thoushallnotpass
Note: Robocorp Lab also allows you to input selectors using the user interface. Here's how it would look like in our case:
As the last step, the robot needs to submit the form, which we can do by adding the Submit Form
keyword:
*** Keywords ***
Log In
Input Text username maria
Input Password password thoushallnotpass
Submit Form
Here's how the complete robot code looks like now:
*** Settings ***
Documentation Robot to enter weekly sales data into the RobotSpareBin Industries Intranet.
Library RPA.Browser
*** Keywords ***
Open The Intranet Website
Open Available Browser https://robotsparebinindustries.com/
*** Keywords ***
Log In
Input Text username maria
Input Password password thoushallnotpass
Submit Form
*** Tasks ***
Insert the sales data for the week and export it as a PDF
Open The Intranet Website
Log In
Run the robot one more time, and you should see it opening the browser, navigating to the intranet, and happily logging in, just like Maria does!
What we learned
- You can use the
Input Text
keyword from theRPA.Browser
library to fill text input fields. - You can tell your robots which elements in a webpage to act on by writing
locators
. - You should use the
Input Password
keyword to fill password fields. - You should not commit credentials directly into your
.robot
files. We are doing it here because it's a beginner's course, but you should never do this in the real world.