find
The find action locates an element in the current interface based on its display text or a CSS/XPath selector. After finding the element, you can optionally interact with it, such as clicking it or typing into it.
If multiple elements match the specified criteria, the step operates on the first matched element. Ensure your criteria uniquely identify the target element.
Specify the target element directly with a string for basic text or selector lookup. Use an object for more detailed options and interactions:
-
String Shorthand: Provide the display text or CSS/XPath selector directly as the value for the
findkey. Doc Detective matches the value in this order and uses the highest-ranked match:- Display text
- Accessible name, such as an
aria-label idattributedata-testidattribute- CSS or XPath selector
If one element’s text and another element’s selector both match, the text match wins. A single word that names an HTML element, such as
TitleorMenu, is also a CSS selector, but an element with that text still wins. To be explicit, use the object format.{ "selector": "#username" }always matches as a selector, and{ "elementText": "Login" }always matches as text.Doc Detective checks all five ways together every 100 milliseconds, for up to 5 seconds, and uses the first check that finds an element. A match that’s already on the page wins over a higher-ranked match that renders later.
To match part of a value with a regular expression, wrap it in slashes, as in
"find": "/Order confirmed/". Doc Detective checks display text first, then accessible name, thenid, thendata-testid. If an element has no text of its own, such as a container, the accessible name check also matches all the text inside it. A regular expression never matches as a CSS or XPath selector. Doc Detective looks right away, then every 100 milliseconds for up to 5 seconds.The match is case-sensitive and takes no flags. It can also match anywhere in the value, so
/Log ?in/matchesLog into your account. To require the whole value, anchor the expression, as in/^Log ?in$/.- Example:
"find": "Login","find": "#username", or"find": "/Log ?in/".
-
Object Format: Use an object with the following properties:
elementText: (Optional) The display text of the element to find. If combined with other element-finding fields, the element must match all of them. See How text matching works below.selector: (Optional) The CSS selector or XPath selector of the element to find. If combined with other element-finding fields, the element must match all of them. At least one element-finding field (elementText,selector,elementId,elementTestId,elementClass,elementAttribute, orelementAria) is required.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 duration in milliseconds to wait for the element to exist (default: 5000).0checks once.moveTo: (Optional) Move the cursor to the element. An element that isn’t visible is scrolled into view. Defaults tofalse. A barefindleaves the viewport and cursor where they’re already sitting. Set this explicitly when a step should scroll, or when a recording needs the cursor to travel to the element.click: (Optional) Click the element after finding it. Can betrue(for a default left click),false(find without clicking),"left","right","middle", or an object like{ "button": "right" }or{ "duration": 800 }(a long-press / press-and-hold, in milliseconds). Here a string names the mouse button, unlike theclickaction, where it names the element. Any other string, such as"click": "Submit", makes the spec invalid. So does an object with more thanbuttonandduration, becausefinditself holds the locators andtimeout. Results show the click that ran as an object, such as{ "button": "right" }.type: (Optional) Type keys after finding the element. Requires the element to be made active first (for example, by usingclick: true). Accepts a string or an object like{ "keys": "my text", "inputDelay": 100 }. Seetypefor details.findalready locates the element, sotypecan’t set its ownselector,elementText, or other locator. One makes the spec invalid.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" }or a specific app window{ "app": "notepad", "window": { "title": "/Open/" } }. 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, Target an app surface, and App windows below.
How text matching works
elementText (and the string shorthand’s text form) matches an element’s
full visible text with whitespace normalized. Doc Detective trims leading and
trailing whitespace and collapses internal runs of spaces, tabs, and newlines to a
single space. Two consequences follow from this:
- Framework-rendered text just works. Frameworks like React and Svelte often
split an element’s text across several DOM text nodes, sometimes with an empty
leading node. The match uses the element’s whole text. A heading like
Garden companion APItherefore matches even when it isn’t a single, tidy text node. The result stays the same on Chrome and Firefox no matter how each browser’s driver reports whitespace. - It’s a whole-element match, not a substring.
elementTextmatches an element whose entire normalized text equals your string. A broadselectorlikebodycombined withelementTexttherefore won’t match, since the body’s text is the whole page. Target a tighter element, or match a substring with a regular expression by wrapping the value in slashes:"elementText": "/Garden companion API/".
App windows
On Windows and macOS app surfaces, window selects which of the app’s windows the step acts on. That includes dialogs and secondary document windows:
"window": { "title": "Create New Data Source" }selects by title; wrap the value in slashes for a regex ("/Data Source/")."window": -1selects the newest window (typically a dialog the app just opened).- A plain string or
{ "name": … }matches the window title exactly. - On macOS, a non-negative
"window": 2selects by position; on Windows, positional indexes aren’t supported (select by title or-1instead).
Window selection is sticky. Later steps on the same app surface act in the selected window until another selector changes it. click, type, screenshot, record, swipe, and closeSurface all accept the same selector. Android and iOS app surfaces are single-window, so a window selector there fails with guidance to omit it.
On Windows, resolving a selector probes the app’s candidate windows to read their titles, which can briefly bring each probed window to the foreground. This matters for recordings: if a record is running, a probe can flash another window into a frame or two of the capture. Select the window before starting the recording (selection is sticky) to avoid it.
Mobile app surfaces auto-scroll. On an Android or iOS app surface, an element that isn’t on screen makes find scroll toward content further down the list before giving up. It takes up to 5 scroll steps, within timeout. That matches how browser find scrolls elements into view. click and element-targeted type locate elements the same way, so they inherit this. For a different gesture, sideways or a precise drag, use swipe.
Setting Variables: to capture a found element’s attributes into variables for later steps, use the step-level variables object. You can assign values based on the element using expressions like $$element.text.
Outputs: the action also exposes outputs.found, a boolean indicating whether an element matching your criteria was located. To capture it for later steps, use the step-level variables object with the expression $$found.
Near matches: when a browser find fails, outputs.nearMatches lists up to three visible elements closest to what you asked for, best first. Each entry has the element’s text and tag, plus its id, testId, and ariaLabel when it has them. An empty list means nothing on the page was close. A /regex/ target gets no near matches. When the list isn’t empty, the step’s resultHint names them, and its resultCode is drift. Otherwise, its resultCode is assertion. See Read the result.
For comprehensive options, see the
findreference.
Examples
The docs tests run every example below except the right-click, middle-click, specific-surface, and several-criteria examples.
Find an element by its text (string shorthand)
Find an element by its selector (string shorthand)
Find an element by a regular expression (string shorthand)
Find an element by selector and click it (object format)
Find an element by selector, click it, and type into it (object format)
Find an element combining selector and text with a timeout
Find an element and right-click it
Find an element and middle-click it
Find by ID, test ID, or accessible name
When an element has no stable text, or a selector would be brittle, target it by its id attribute (elementId), its data-testid (elementTestId), or its computed accessible name (elementAria). 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. The example below finds a button by its test ID. The forms { "elementId": "submit-button" } and { "elementAria": "Save document button" } work the same way.
Find by class and attribute
Combine elementClass with elementAttribute to pin down an element by how it’s styled and marked up. elementClass accepts a single class or an array of classes that must all be present. elementAttribute matches attribute values as strings (with /regex/ support), numbers, or booleans, where true requires the attribute to be present and false requires it to be absent.
Match an element with a regular expression
elementText, elementId, elementTestId, elementClass, elementAttribute, and elementAria all match a substring by regular expression when you wrap the value in slashes. This is handy for IDs or text that carry a variable part, such as a generated suffix.
Move the cursor to an element
Set moveTo to true to move the cursor to the element. If the element isn’t visible, Doc Detective scrolls it into view. A bare find leaves the viewport and cursor in place, so set moveTo when a step should scroll or when a recording needs the cursor to travel.
Find, click, and type using the object format
Pass type an object to set an inputDelay alongside the keys, rather than the string form. Use click: true first so the element is active before you send the keys.
Find an element in a specific tab or app surface
Set surface to act on an element outside the active surface. A browser reference selects the window or tab the element lives in. An app reference targets a control on a native app surface. Omit surface to act on the active surface.
Find using several criteria at once
Element-finding fields combine with AND semantics: the element must match every field you set. This example narrows to a single element by ID, class, and attribute, then moves the cursor to it and clicks. Use elementAttribute boolean true to require an attribute’s presence.