click
The click action allows you to click or tap an element on the page. You can specify which mouse button to use and target elements using text or selectors.
The click action works in several ways:
- String Shorthand: The display text or selector of the element to find and click.
- Object Format: Use an object with detailed properties for more control over the click action.
click needs an element, so "click": true and "click": false fail the step. To click an element whose text is true or false, quote the value: "click": "true".
To find an element before clicking it, consider the find action. It locates elements and optionally clicks them in a single step.
A click string always names the element, so "click": "right" clicks the element whose text is “right”. To choose the mouse button, use the object format’s button. In find, a click string names the button instead.
click needs an element, so "click": true and "click": false make the spec invalid, and the run exits 2. To click an element whose text is true or false, quote the value: "click": "true".
Properties
When using the object format, you can specify:
button: (Optional) Kind of click to perform. Can be"left","right", or"middle". Default is"left".duration: (Optional) How long to hold the press, in milliseconds. It’s a long-press on mobile app surfaces and a press-and-hold of the primary button on desktop apps. On browsers it’s a press-and-hold of the givenbutton. Omit it for a normal click. A long-press on an app surface is primary-button only, sodurationwith a non-leftbuttonfails the step. On browsers, a held click needs the W3C actions endpoint. Like non-left buttons, it’s desktop-browser-only.selector: (Optional) CSS selector or XPath selector of the element to click. If combined with other element-finding fields, the element must match all of them.elementText: (Optional) Display text of the element to click. If combined with other element-finding fields, the element must match all of them.elementId: (Optional) ID attribute of the element. Supports exact match or a regex pattern using/pattern/syntax.elementTestId: (Optional)data-testidattribute of the element. Supports exact match or a regex pattern using/pattern/syntax.elementClass: (Optional) Class, or array of classes, the element must have. Each class supports exact match or a/pattern/regex. The element must have all specified classes.elementAttribute: (Optional) Object of attribute key-value pairs the element must have. Values can be strings (supporting/pattern/regex), numbers, or booleans. Booleantruematches attribute presence, andfalsematches absence.elementAria: (Optional) Computed accessible name of the element per the ARIA specification. Supports exact match or a/pattern/regex.timeout: (Optional) Maximum time in milliseconds to wait for the element to exist. Default is5000.0checks once.surface: (Optional) The browser session, window, or tab the element lives in, such as{ "browser": "chrome", "tab": "cart" }or a named browser{ "browser": "firefox", "name": "admin" }. It can also be the native app surface the element lives in, such as{ "app": "notepad" }. Omit it to act on the active surface, the most recently opened, focused, or explicitly targeted one, whether a browser tab or an app window. Specifying a surface switches the active surface for the steps that follow. On an app surface,elementText,elementId, andelementAriamap to native controls.selectorthere accepts a native locator, either XPath or a~-prefixed accessibility ID, instead of a CSS selector. See Test across multiple tabs, windows, and browsers and Target an app surface.
Outputs: when you target an element by text or selector, the action exposes outputs.found, a boolean indicating whether the element was located. To capture it for later steps, use the step-level variables object with the expression $$found. When a browser click can’t find its element, outputs.nearMatches lists the closest visible elements, as it does for find. When the list isn’t empty, the step’s resultHint names them, and its resultCode is drift. Otherwise, its resultCode is assertion.
For comprehensive options, see the full
clickreference.
Examples
The docs tests run the two string shorthand examples and the object-format example that clicks by text. They also run the ID, regular-expression, and right-click examples. The other examples are illustration-only.
Click by element text (string shorthand)
Click by selector (string shorthand)
Click an element by text (object format)
Click an element by selector (object format)
Click an element by both selector and text
Click by ID, test ID, or accessible name
Target an element by its id attribute (elementId), its data-testid (elementTestId), or its computed accessible name (elementAria) when its text isn’t unique or stable. The accessible name is the one your browser’s DevTools accessibility panel reports for the element. Each takes an exact value or a /regex/ pattern, and elementClass and elementAttribute match the same way as they do for find. The example below clicks by test ID. The forms { "click": { "elementId": "submit-button" } }, { "click": { "elementAria": "Save document button" } }, { "click": { "elementClass": "btn-primary" } }, and { "click": { "elementAttribute": { "data-role": "submit" } } } work the same way.
Match the element with a regular expression
Wrap a value in slashes to match a substring by regular expression instead of the whole text.
Long-press an element on a mobile app surface
Set duration on an app surface to press and hold an element instead of tapping it.
Right-click an element
Set button to "right" to open a context menu or trigger a right-click handler.
Press and hold on a browser
Set duration to hold the press for that many milliseconds. On a browser, a held click, like a non-left button, runs on desktop browsers only.
Click in a specific browser tab
Set surface to a browser reference to click an element outside the active tab. Omit surface to act on the active surface.
Mobile app surfaces
On an app surface, click is a tap, and duration makes it a long-press. The button field (left, right, or middle) works where the platform’s driver supports it. Windows honors all three. macOS honors right-click and rejects middle-click, which the Mac2 driver can’t do. Android and iOS reject any non-left button with a clear message, because touch input has no right or middle button. On Windows, plain left clicks activate the element through the UI Automation Invoke pattern. That’s coordinate-free, so display scaling can’t misdirect them. They fall back to a physical click for elements that don’t support it. Non-left and held clicks stay physical. To click in a specific window of the app, add a window selector. See App windows. Two more patterns are worth knowing:
- Permission dialogs are just elements. When your documentation walks the reader through granting a permission, click the dialog’s button like any other element:
{ "click": { "elementText": "Allow", "surface": { "app": "myapp" } } }. System dialogs expose their buttons through the platform accessibility tree, soelementTextmatches them. - When the dialog isn’t the point, suppress it at session start instead of scripting around it. Pass the driver’s own knob through
startSurface.driverOptions, such as{ "driverOptions": { "appium:autoGrantPermissions": true } }on Android. Use this when the procedure you’re testing starts after the reader grants permissions.