robocorp-windows
Quickstart
Getting started with robocorp.windows
is as simple as:
Now, if we put this in a Task/Action Package, we could save a tasks.py file with the following content:
Locators
One of the key concepts in the library are locators. A locator is a string which identifies how to reach a given element to interact with. This string can have multiple entries separated by spaces such as:
<property_name>:"<property value>" <property_name>:"<property value>"
Note: if the <property value>
itself doesn't have spaces the "
is not needed.
It's also possible to consider the parent/child hierarchy so that multiple
matches are done when using >
. Example: name:Calculator > class:TextBlock
.
Also, it's possible to use or
constructs to create more advanced matches.
Example: (name:0 or name:Zero) and class:Button
.
Note: >
must always be top-level and may not appear inside an or
.
The property names available for matching are:
-
name
: identifies a target window/control by itsname
. Example:name:"My Window"
. -
regex
: identifies a target window/control by itsname
matching using a regexp. Example:regex:".*Calc.*"
. -
subname
identifies a target window/control by itsname
matching using thein
operator. Example:subname:cal"
. -
class
: identifies a target window/control by itsclass
. Example:class:Button
,class:TextBlock
. -
control
(may also be used astype
): identifies a target window/control by itstype
. Example:control:ButtonControl
,type:ButtonControl
. -
id
(may also be used asautomationid
): identifies a target window/control by itsautomation id
. Example:id:"open button"
. -
executable
: identifies a target window by its executable name (may be the full path or just basename). Example:executable:notepad.exe
) -
handle
: the target window handle. Example:handle:21345
. -
path
: identifies a target element by its index-based path traversal from the parent. Examplepath:2|3|8|2
. -
depth
: identifies a target element by its depth from the parent. Exampledepth:2
.
It's also possible to get an element and then go deeper in the structure to have the same result.
i.e.: Using:
is the same as:
In general the recommended approach is getting the top-level window of interest
with windows.find_window
and then getting sub-elements as needed (and if
multiple elements have the same parent, first finding the parent and then getting
children elements from that parent for faster access).
Note: when querying the information, APIs may have a search_depth
or max_depth
parameter which can be specified to determine up to which depth a given element
may be found. It's not recommended to have a big depth value as bigger depths
mean that more items have to be traversed to find an element which can make
such searches slower (so, more queries with a shalower depth is recommended
over less queries with a bigger depth).
Working with locators in robocorp-windows
The term locator
means a string which identifies how to reach an
element in the application UI structure.
In the context of robocorp-windows
it'll identify how to reach a window
or some element inside the window (such as a combo box, text edit, etc).
With robocorp-windows
, a basic inspector which can be used to identify
the values which can be used in locators as well as testing locators can
be used with the inspect()
method for some element.
To start from the desktop, the API used would be:
Then, using the select window
(activated with s:<optional window-related name>
--
i.e.: s:note
to filter for windows with note
in the name)
can be used to inspect elements inside a given window
(follow the text prompts to know what's available).
After you have the locator for the window, it's recommended to start from the window directly:
Example with Notepad++
:
Usually the most useful approach is then asking for the highlight mouse
(activated with m
) and then hovering over the element of interest -- after
a timeout with the mouse in the same position the related element should be
highlighted and the information available to be used to reach it will be
printed to the output.
Example:
Hovering over the record button of Notepad++ will print something as:
In this case some valid locators to reach it could be:
name:"Start Recording"
control:ButtonControl
class:ToolbarWindow32 > control:ButtonControl and name:"Start Recording"
path:5|1|35
Note that it's important to select a locator that will uniquely identify the
element (so, for instance the control:ButtonControl
may not be ideal because
it may match other buttons in the UI).
Also, it's important to note that when a given element is printed, in the inspector you can interact with it to know whether it's actually valid, so, for instance, it's possible to test a locator with:
to see which elements would be selected by it.
For the particular case of Notepad++, you may notice that doing something
as h:name:"Start Recording"
may fail to actually highlight any element.
In the Notepad++ case this happens because the buttons in the toolbar will only be accessible when the mouse is hovering over the toolbar.
In this case, the code to successfully access it would need to first hover over the toolbar:
Also note that if you're targetting multiple versions of some application,
it's possilbe to use more advanced expressions with or
and and
conditions
(by default the and
is not needed as a space
will mean and
implicitly).
For instance, the windows calculator has different ways to reach buttons
depending on the Windows version, on some versions the name to reach
it would be name:0
or name:Zero
. In this case it'd be possible to
write a locator such as class:Button and (name:0 or name:Zero)
.
Special keys
It's possible to send special keys (using the send_keys method)
by wrapping the related code below with {}
(Example: {Ctrl}
, {Enter}
).
Key code | Key description |
---|---|
LBUTTON | Left mouse button |
RBUTTON | Right mouse button |
CANCEL | Control-break processing |
MBUTTON | Middle mouse button (three-button mouse) |
XBUTTON1 | X1 mouse button |
XBUTTON2 | X2 mouse button |
BACK | BACKSPACE key |
TAB | TAB key |
CLEAR | CLEAR key |
RETURN | ENTER key |
ENTER | ENTER key |
SHIFT | SHIFT key |
CTRL | CTRL key |
CONTROL | CTRL key |
ALT | ALT key |
PAUSE | PAUSE key |
CAPITAL | CAPS LOCK key |
KANA | IME Kana mode |
HANGUEL | IME Hanguel mode (maintained for compatibility; use VK_HANGUL) |
HANGUL | IME Hangul mode |
JUNJA | IME Junja mode |
FINAL | IME final mode |
HANJA | IME Hanja mode |
KANJI | IME Kanji mode |
ESC | ESC key |
ESCAPE | ESC key |
CONVERT | IME convert |
NONCONVERT | IME nonconvert |
ACCEPT | IME accept |
MODECHANGE | IME mode change request |
SPACE | SPACEBAR |
PRIOR | PAGE UP key |
PAGEUP | PAGE UP key |
NEXT | PAGE DOWN key |
PAGEDOWN | PAGE DOWN key |
END | END key |
HOME | HOME key |
LEFT | LEFT ARROW key |
UP | UP ARROW key |
RIGHT | RIGHT ARROW key |
DOWN | DOWN ARROW key |
SELECT | SELECT key |
PRINT key | |
EXECUTE | EXECUTE key |
SNAPSHOT | PRINT SCREEN key |
PRINTSCREEN | PRINT SCREEN key |
INSERT | INS key |
INS | INS key |
DELETE | DEL key |
DEL | DEL key |
HELP | HELP key |
WIN | Left Windows key (Natural keyboard) |
LWIN | Left Windows key (Natural keyboard) |
RWIN | Right Windows key (Natural keyboard) |
APPS | Applications key (Natural keyboard) |
SLEEP | Computer Sleep key |
NUMPAD0 | Numeric keypad 0 key |
NUMPAD1 | Numeric keypad 1 key |
NUMPAD2 | Numeric keypad 2 key |
NUMPAD3 | Numeric keypad 3 key |
NUMPAD4 | Numeric keypad 4 key |
NUMPAD5 | Numeric keypad 5 key |
NUMPAD6 | Numeric keypad 6 key |
NUMPAD7 | Numeric keypad 7 key |
NUMPAD8 | Numeric keypad 8 key |
NUMPAD9 | Numeric keypad 9 key |
MULTIPLY | Multiply key |
ADD | Add key |
SEPARATOR | Separator key |
SUBTRACT | Subtract key |
DECIMAL | Decimal key |
DIVIDE | Divide key |
F1 | F1 key |
F2 | F2 key |
F3 | F3 key |
F4 | F4 key |
F5 | F5 key |
F6 | F6 key |
F7 | F7 key |
F8 | F8 key |
F9 | F9 key |
F10 | F10 key |
F11 | F11 key |
F12 | F12 key |
F13 | F13 key |
F14 | F14 key |
F15 | F15 key |
F16 | F16 key |
F17 | F17 key |
F18 | F18 key |
F19 | F19 key |
F20 | F20 key |
F21 | F21 key |
F22 | F22 key |
F23 | F23 key |
F24 | F24 key |
NUMLOCK | NUM LOCK key |
SCROLL | SCROLL LOCK key |
LSHIFT | Left SHIFT key |
RSHIFT | Right SHIFT key |
LCONTROL | Left CONTROL key |
LCTRL | Left CONTROL key |
RCONTROL | Right CONTROL key |
RCTRL | Right CONTROL key |
LALT | Left MENU key |
RALT | Right MENU key |
BROWSER_BACK | Browser Back key |
BROWSER_FORWARD | Browser Forward key |
BROWSER_REFRESH | Browser Refresh key |
BROWSER_STOP | Browser Stop key |
BROWSER_SEARCH | Browser Search key |
BROWSER_FAVORITES | Browser Favorites key |
BROWSER_HOME | Browser Start and Home key |
VOLUME_MUTE | Volume Mute key |
VOLUME_DOWN | Volume Down key |
VOLUME_UP | Volume Up key |
MEDIA_NEXT_TRACK | Next Track key |
MEDIA_PREV_TRACK | Previous Track key |
MEDIA_STOP | Stop Media key |
MEDIA_PLAY_PAUSE | Play/Pause Media key |
LAUNCH_MAIL | Start Mail key |
LAUNCH_MEDIA_SELECT | Select Media key |
LAUNCH_APP1 | Start Application 1 key |
LAUNCH_APP2 | Start Application 2 key |
OEM_1 | Used for miscellaneous characters; it can vary by keyboard.For the US standard keyboard, the ';:' key |
OEM_PLUS | For any country/region, the '+' key |
OEM_COMMA | For any country/region, the ',' key |
OEM_MINUS | For any country/region, the '-' key |
OEM_PERIOD | For any country/region, the '.' key |
OEM_2 | Used for miscellaneous characters; it can vary by keyboard. |
OEM_3 | Used for miscellaneous characters; it can vary by keyboard. |
OEM_4 | Used for miscellaneous characters; it can vary by keyboard. |
OEM_5 | Used for miscellaneous characters; it can vary by keyboard. |
OEM_6 | Used for miscellaneous characters; it can vary by keyboard. |
OEM_7 | Used for miscellaneous characters; it can vary by keyboard. |
OEM_8 | Used for miscellaneous characters; it can vary by keyboard. |
OEM_102 | Either the angle bracket key or the backslash key on the RT 102-key keyboard |
PROCESSKEY | IME PROCESS key |
PACKET | Used to pass Unicode characters as if they were keystrokes. The VK_PACKET key is the low word of a 32-bit Virtual Key value used for non-keyboard input methods. For more information, see Remark in KEYBDINPUT, SendInput, WM_KEYDOWN, and WM_KeyUp |
ATTN | Attn key |
CRSEL | CrSel key |
EXSEL | ExSel key |
EREOF | Erase EOF key |
PLAY | Play key |
ZOOM | Zoom key |
NONAME | Reserved |
PA1 | PA1 key |
OEM_CLEAR | Clear key |