RPA.Desktop

Desktop is a cross-platform library for navigating and interacting with desktop environments. It can be used to automate applications through the same interfaces that are available to human users.

The library includes the following features:

  • Mouse and keyboard input emulation
  • Starting and stopping applications
  • Finding elements through image template matching
  • Scraping text from given regions
  • Taking screenshots
  • Clipboard management

Warning

Windows element selectors are not currently supported, and require the use of RPA.Desktop.Windows

Installation

The basic features such as mouse and keyboard input and application control work with a default rpaframework install.

Advanced computer-vision features such as image template matching and OCR require an additional library called rpaframework-recognition.

The dependency should be added separately by specifing it in your conda.yaml as rpaframework-recognition==5.0.1 for example. If installing recognition through pip instead of conda, the OCR feature also requires tesseract.

Locating elements

To automate actions on the desktop, a robot needs to interact with various graphical elements such as buttons or input fields. The locations of these elements can be found using a feature called locators.

A locator describes the properties or features of an element. This information can be later used to locate similar elements even when window positions or states change.

The currently supported locator types are:

NameArgumentsDescription
aliasname (str)A custom named locator from the locator database, the default.
imagepath (str)Image of an element that is matched to current screen content.
pointx (int), y (int)Pixel coordinates as absolute position.
offsetx (int), y (int)Pixel coordinates relative to current mouse position.
sizewidth (int), height (int)Region of fixed size, around point or screen top-left
regionleft (int), top (int), right (int), bottom (int)Bounding coordinates for a rectangular region.
ocrtext (str), confidence (float, optional)Text to find from the current screen.

A locator is defined by its type and arguments, divided by a colon. Some example usages are shown below. Note that the prefix for alias can be omitted as its the default type.

Click point:50,100 Click region:20,20,100,30 Move mouse image:%{ROBOT_ROOT}/logo.png Move mouse offset:200,0 Click Click alias:SpareBin.Login Click SpareBin.Login Click ocr:"Create New Account"

You can also pass internal region objects as locators:

${region}= Find Element ocr:"Customer name" Click ${region}

Locator chaining

Often it is not enough to have one locator, but instead an element is defined through a relationship of various locators. For this use case the library supports a special syntax, which we will call locator chaining.

An example of chaining:

# Read text from area on the right side of logo Read text image:logo.png + offset:600,0 + size:400,200

The supported operators are:

OperatorDescription
then, +Base locator relative to the previous one
and, &&, &Both locators should be found
or, ||, |Either of the locators should be found
not, !The locator should not be found

Further examples:

# Click below either label Click (image:name.png or image:email.png) then offset:0,300 # Wait until dialog disappears Wait for element not image:cookie.png

Named locators

The library supports storing locators in a database, which contains all of the required fields and various bits of metadata. This enables having one source of truth, which can be updated if a website's or applications's UI changes. Robot Framework scripts can then only contain a reference to a stored locator by name.

The main way to create named locators is with VSCode.

Read more on identifying elements and crafting locators:

Keyboard and mouse

Keyboard keywords can emulate typing text, but also pressing various function keys. The name of a key is case-insensitive and spaces will be converted to underscores, i.e. the key Page Down and page_down are equivalent.

The following function keys are supported:

KeyDescription
shiftA generic Shift key. This is a modifier.
shift_lThe left Shift key. This is a modifier.
shift_rThe right Shift key. This is a modifier.
ctrlA generic Ctrl key. This is a modifier.
ctrl_lhe left Ctrl key. This is a modifier.
ctrl_rThe right Ctrl key. This is a modifier.
altA generic Alt key. This is a modifier.
alt_lThe left Alt key. This is a modifier.
alt_rThe right Alt key. This is a modifier.
alt_grThe AltGr key. This is a modifier.
cmdA generic command button (Windows / Command / Super key). This may be a modifier.
cmd_lThe left command button (Windows / Command / Super key). This may be a modifier.
cmd_rThe right command button (Windows / Command / Super key). This may be a modifier.
upAn up arrow key.
downA down arrow key.
leftA left arrow key.
rightA right arrow key.
enterThe Enter or Return key.
spaceThe Space key.
tabThe Tab key.
backspaceThe Backspace key.
deleteThe Delete key.
escThe Esc key.
homeThe Home key.
endThe End key.
page_downThe Page Down key.
page_upThe Page Up key.
caps_lockThe Caps Lock key.
f1 to f20The function keys.
insertThe Insert key. This may be undefined for some platforms.
menuThe Menu key. This may be undefined for some platforms.
num_lockThe Num Lock key. This may be undefined for some platforms.
pauseThe Pause / Break key. This may be undefined for some platforms.
print_screenThe Print Screen key. This may be undefined for some platforms.
scroll_lockThe Scroll Lock key. This may be undefined for some platforms.

When controlling the mouse, there are different types of actions that can be done. Same formatting rules as function keys apply. They are as follows:

ActionDescription
clickClick with left mouse button
left_clickClick with left mouse button
double_clickDouble click with left mouse button
triple_clickTriple click with left mouse button
right_clickClick with right mouse button

The supported mouse button types are left, right, and middle.

Examples

Both Robot Framework and Python examples follow.

The library must be imported first.

*** Settings *** Library RPA.Desktop
from RPA.Desktop import Desktop desktop = Desktop()

The library can open applications and interact with them through keyboard and mouse events.

*** Keywords *** Write entry in accounting [Arguments] ${entry} Open application erp_client.exe Click image:%{ROBOT_ROOT}/images/create.png Type text ${entry} Press keys ctrl s Press keys enter
def write_entry_in_accounting(entry): desktop.open_application("erp_client.exe") desktop.click(f"image:{ROBOT_ROOT}/images/create.png") desktop.type_text(entry) desktop.press_keys("ctrl", "s") desktop.press_keys("enter")

Targeting can be currently done using coordinates (absolute or relative), but using template matching is preferred.

*** Keywords *** Write to field [Arguments] ${text} Move mouse image:input_label.png Move mouse offset:200,0 Click Type text ${text} Press keys enter
def write_to_field(text): desktop.move_mouse("image:input_label.png") desktop.move_mouse("offset:200,0") desktop.click() desktop.type_text(text) desktop.press_keys("enter")

Elements can be found by text too.

*** Keywords *** Click New Click ocr:New
def click_new(): desktop.click('ocr:"New"')

It is recommended to wait for the elements to be visible before trying any interaction. You can also pass region objects as locators.

*** Keywords *** Click New ${region}= Wait For element ocr:New Click ${region}
def click_new(): region = desktop.wait_for_element("ocr:New") desktop.click(region)

Another way to find elements by offsetting from an anchor:

*** Keywords *** Type Notes [Arguments] ${text} Click With Offset ocr:Notes 500 0 Type Text ${text}
def type_notes(text): desktop.click_with_offset("ocr:Notes", 500, 0) desktop.type_text(text)

Importing

Initialize self. See help(type(self)) for accurate signature.