Locator Strategies
Choosing a locator strategy
QWeb is designed to be text-first, encouraging test cases that describe user interactions rather than the underlying implementation of a web application.
In QWeb, the locator strategy is typically expressed through the keyword name itself (*Text, *Item, *Element, *Icon). Each keyword family has search rules suited to its locator type. For example, ClickText prefers clickable elements, while ClickElement targets the element matched by an explicit selector. This makes the intent of a test case clear without requiring the reader to interpret locator syntax.
Some keywords, such as TypeText and GetText, support multiple locator types for added flexibility (see section 5).
| Strategy | Purpose | Example | Shadow DOM |
|---|---|---|---|
| Text | Identify elements by visible text | ClickText Save |
Yes |
| Item | Identify elements by attribute values | ClickItem save-button |
Yes |
| Element (CSS) | Identify elements using CSS selectors | ClickElement css\=button#save |
Yes |
| Element (XPath) | Identify elements using XPath expressions | ClickElement //button[@id\="save"] |
No |
| Icon | Identify elements by their visual appearance | ClickIcon save.png |
Independent of DOM |
Prefer the simplest locator that uniquely and reliably identifies the intended element.
Text and Item locators are generally preferred because they keep test cases readable and reduce dependencies on the underlying implementation. CSS and XPath provide greater control when more precise element identification is needed. Image-based locators provide an alternative when elements cannot be reliably identified through the DOM.
These strategies represent different levels of abstraction, not a strict hierarchy of reliability. A well-constructed CSS selector using stable attributes may be more reliable than a text locator in certain applications.
Higher abstraction reduces unnecessary dependencies on implementation details. Explicit selectors provide greater precision when those details matter.
Shadow DOM support
Text, Item and CSS locators can search within open shadow roots when Shadow DOM support is enabled using SetConfig ShadowDOM True. XPath cannot locate elements inside native shadow roots. Image-based locators operate on the rendered page and are independent of DOM boundaries.
1. Text Locators (Preferred)
Keywords: ClickText, VerifyText, HoverText
The simplest way to locate an element is often by its visible text.
Example: Imagine a "Sign In" button that logs in and takes you to a dashboard page.
- Accuracy: High when the text is unique. Duplicate text and partial matching may require anchors, indexing or exact matching.
- Resilience: Independent of the underlying HTML structure, provided the visible text remains unchanged and the element remains eligible for matching.
- Maintainability: Human-readable locators are easy to understand and update when UI labels change.
Controlling Precision
Partial Matching: QWeb defaults to substring matching. This means ClickText Save will match a button labeled "Save Changes". If partial matching causes issues (e.g., clicking "Save All" when you wanted "Save"), you can enforce exact matches:
- Per Keyword:
ClickText Save partial_match=False - Globally:
SetConfig PartialMatch False
2. Item Locators (Attributes)
Keywords: ClickItem, VerifyItem, VerifyNoItem, HoverItem, IsItem, ClickItemWhile, ClickItemUntil
When an element has no visible text (like an icon, a trash can button, or a logo), or cannot be reliably identified by its text alone, Item keywords provide a convenient way to locate it by its attribute values. These keywords match any attribute value of common elements such as links, buttons, images, icons and list items. Typical attributes that describe an element are:
title(Tooltip text)alt(Image description)aria-label(Accessible name)data-icon,id, or any other attribute
Example:
Imagine a "Clear Search" button that is just an 'X' icon but has title="Clear".
- Accuracy: High when descriptive attribute values are unique. Reused values may require additional filtering through
tagorindex. - Resilience: Independent of DOM structure and specific attribute names. Locators can remain functional even when a value moves from one attribute to another, for example from
data-testidtoaria-label. - Maintainability: Short, descriptive attribute values keep locators readable and reduce dependencies on implementation details.
Limiting elements using the tag argument
Narrow your attribute search to specific element types by specifying the tag argument.
# Matches only elements with tag "custom_input" and attribute value "Reset"
ClickItem Reset tag=custom_input
Using the tag argument can also improve search performance.
3. Element Locators (XPath or CSS)
Keywords: ClickElement, VerifyElement, VerifyNoElement, IsElement, RightClick, HoverElement, GetElementCount (and GetWebElement, GetAttribute, VerifyAttribute, GetCoordinates)
When more precise element identification is needed, Element keywords provide full control through XPath and CSS selectors. These allow matching specific attributes, combining multiple conditions and expressing structural relationships between elements.
For Element keywords, an explicit selector without a prefix is interpreted as XPath (see section 5 for attribute-based matching with tag). Start the locator with css\= to use a CSS selector. GetText, VerifyElementText and the input keywords accept css\= locators too (see section 5).
# Using XPath
ClickElement //div[@class\="user-menu"]//button
ClickElement xpath\=//div[@class\="user-menu"]//button
# Using CSS
ClickElement css\=div.user-menu button
ClickElement css\=input[name\="email"]
ClickElement css\=li.item index=2
A css\= locator works like an XPath locator: the keyword waits until the timeout, uses the same visibility rules, searches all frames and picks a match with index. ClickElement clicks the matched element itself; it doesn't look for a clickable parent or child. A selector with invalid syntax (for example css\=div[) fails at once instead of waiting for the timeout.
When to use CSS instead of Item locators
Item locators search attribute values without requiring a specific attribute name. CSS selectors allow you to target named attributes and combine multiple conditions:
ClickItem Save tag=button
ClickElement css\=button[aria-label\="Save"]
ClickElement css\=button[aria-label\="Save"][type\="submit"]
Use CSS when the additional precision is needed to distinguish the intended element from other matches.
QWeb-specific CSS text filters
Standard CSS cannot match visible text directly, so QWeb allows a css\= locator to end with one text filter. These are QWeb extensions, not standard CSS pseudo-classes, and cannot be used directly with browser APIs such as document.querySelector():
:has-text("..."): the element's visible text contains the given text.:text-is("..."): the element's whole visible text equals the given text.
ClickElement css\=div.card:has-text("Hello")
VerifyElement css\=button:text-is("Save")
VerifyElement css\=p:has-text('Say "hi"')
- Matching is always case-sensitive, and whitespace is trimmed and collapsed before comparing. The
PartialMatchandCaseInsensitivesettings and thepartial_matchargument don't affect text filters. - If one match contains another (a card inside a card), only the innermost match is kept.
indexis applied after that. On a normal page, containment stops at a real shadow root. Salesforce Lightning synthetic shadow hides that relationship from the usual DOM check, so there QWeb uses the physical tree: a component wrapper and the element inside it do not both count. - The filter must be the last part of the selector, only one filter is allowed, and it can't be combined with a selector list (
a, b). - The text can be in double or single quotes. To escape a quote inside the same kind of quotes, write
\\"in Robot Framework source. Write two or more consecutive spaces as${SPACE}.
tag argument
tag= is ignored with a css\= locator (a warning is logged). Put the tag or attribute in the selector instead: css\=button[data-id\="save"].
element_type=css is deprecated
GetWebElement button.primary element_type=css still works but logs a deprecation warning. Use GetWebElement css\=button.primary instead.
CSS and Shadow DOM
With SetConfig ShadowDOM True, CSS locators find elements inside open shadow roots, including nested ones. Closed shadow roots are not supported.
Each selector is matched inside one real shadow root at a time (or the light DOM). A selector cannot continue from one tree into another, and shadow-piercing syntax such as >> or >>> is not supported. To refer to the shadow host from inside its shadow root, use :host().
Salesforce Lightning synthetic shadow is different: the markup stays in the document, and css\= sees it without ShadowDOM, the same elements an XPath locator sees. See Synthetic shadow.
For example, suppose a settings-panel element is a shadow host, and its shadow root contains a div.actions with a Save button:
# Works: .actions and button are in the same shadow root
ClickElement css\=.actions button:text-is("Save")
# Fails: settings-panel is the shadow host, .actions is inside its shadow root
ClickElement css\=settings-panel .actions button
# Works: :host() names the shadow host from inside
ClickElement css\=:host(settings-panel) .actions button
XPath and native Shadow DOM
XPath cannot find elements inside a real shadow root. It does see Lightning synthetic-shadow content, as css\= does. Use Text, Item or CSS locators for a real shadow root. See Shadow DOM for more information.
- Accuracy: Provides precise control over element identification through explicit matching conditions.
- Resilience: Depends on selector construction. Stable attribute-based selectors can be highly resilient, while dependencies on specific attribute names, DOM hierarchy or element positions introduce additional sensitivity to implementation changes.
- Maintainability: Simple attribute-based selectors are easy to maintain. Complex structural selectors may require more effort to understand and update.
Syntax Note
Escape every equals sign in an XPath or CSS locator with a backslash (\=), including the one in the xpath\= and css\= prefixes. Otherwise Robot Framework reads the locator as a named argument and the keyword fails with an argument error.
4. Image Locators (Bitmap)
Keywords: ClickIcon, VerifyIcon
When elements cannot be reliably identified through the DOM, QWeb can locate them by comparing a reference image against the screen.
- Accuracy: Depends on matching tolerance, rendering and reference image quality.
- Resilience: Sensitive to resolution, display scaling, theme changes and rendering differences.
- Maintainability: Visual changes may require recapturing reference images and revalidating them across environments.
5. Keywords that work with several locator types
Some keywords are not tied to one family:
- Input keywords (
TypeText,TypeSecret,VerifyInputValue,VerifyInputStatus,VerifyInputElement,GetInputValue,UploadFile,PressKey) find the field by its label text, by an attribute value such as a placeholder, by an XPath (//orxpath\=prefix) or by acss\=locator. With an XPath orcss\=locator the matched element itself is used, so it must be the input, textarea or editable element;indexpicks the match. - Dropdown and checkbox keywords (
DropDown,VerifySelectedOption,ClickCheckbox, ...) find the field by its label text, by an attribute value or by an XPath. GetWebElement,GetAttribute,VerifyAttributeandGetCoordinatesuse an XPath or acss\=locator by default. Withelement_typethey search like the other families:element_type=text,item,input,dropdownorcheckbox.GetTextandVerifyElementTextfind the element by its text, by an attribute value withtag, by an XPath (containing//) or by acss\=locator. With acss\=locator,anchoris the index of the match andtagis ignored.- ...Element keywords also accept an attribute value with
tag, e.g.ClickElement click_me tag=button.
TypeText Username myusername@test.com # label text
TypeText xpath\=//input[@id\="user"] myusername@test.com
TypeText css\=input#user myusername@test.com
${elem}= GetWebElement Log In element_type=text
${elem}= GetWebElement css\=button.login
${price}= GetText css\=#cart .total
VerifyElementText css\=li.item Item 2 anchor=2
${value}= GetAttribute Country value element_type=dropdown