Elements¶
Element is a lazy selector bound to a device: nothing is looked up until an action or query
runs. State reads and actions require exactly one match; exists() and count() accept any
number. element.wait() returns an ElementWait. app.elements(selector) and
device.screen.elements(selector) return a read-only Elements collection for bulk values. Guide:
Elements and text, Gestures,
Waits.
Element
¶
A selector bound to a device.
Nothing is resolved until a method is called; every call sends the selector to the device
again, so an Element can be kept for the whole test. Actions require exactly one match
and raise CommandError (AMBIGUOUS/NOT_FOUND) before any input otherwise.
Methods:
| Name | Description |
|---|---|
exists |
True when at least one node matches right now (any number of matches is fine). |
count |
Matches in the selector's scope right now, ignoring its match limit. |
snapshot |
State of the one matching node at this instant (AMBIGUOUS/NOT_FOUND otherwise). |
text |
Raw accessibility text of the one matching node, or None when it has none. On API 26+ |
is_enabled |
|
is_checked |
|
tap |
Tap |
triple_tap |
Three taps with |
long_tap |
Long press on one matching node. |
double_tap |
Two taps at the centre of the one matching node's visible bounds, inside Android's |
tap_outside_panel |
Tap outside the panel this element is in: a dialog, popup, menu, drawer or bottom |
ime_action |
Run the one matching text field's keyboard action (Search, Go, Send, Done, … as the |
perform_action |
Perform a standard accessibility |
perform_custom_action |
Perform the custom accessibility action labelled |
set_progress |
Set the one matching range node (SeekBar, Slider, RatingBar) to |
drag_to |
Press the one matching node until it is a long press, move the finger to the centre of |
pinch_open |
Two fingers moving apart from the one matching node's centre, across |
pinch_close |
Two fingers moving together towards the one matching node's centre, across |
fling |
One fast swipe across the one matching node towards |
set_text |
Accessibility text replacement ( |
type_text |
Tap the one matching node, wait until it reports focus, then type |
clear_text |
|
swipe |
One swipe gesture across the node in the direction the finger moves ( |
scroll |
Scroll towards the content edge (DOWN reveals content below). |
scan |
Collect bounded descendant observations from the current scroll position. |
scroll_until |
Client-side loop: checks whether |
wait |
An |
descendant |
The node matching |
child |
The direct child of this node matching |
first |
Accept the first match in accessibility order instead of requiring exactly one. |
at |
Accept the |
exists
¶
exists(timeout: float | None = None) -> bool
True when at least one node matches right now (any number of matches is fine).
count
¶
count(timeout: float | None = None) -> int
Matches in the selector's scope right now, ignoring its match limit.
snapshot
¶
snapshot(timeout: float | None = None) -> ElementSnapshot
State of the one matching node at this instant (AMBIGUOUS/NOT_FOUND otherwise).
text
¶
text(timeout: float | None = None) -> str | None
Raw accessibility text of the one matching node, or None when it has none. On API 26+
an empty field reports its hint here; snapshot().showing_hint says so.
is_enabled
¶
is_enabled(timeout: float | None = None) -> bool
snapshot().enabled of the one matching node.
is_checked
¶
is_checked(timeout: float | None = None) -> bool
snapshot().checked of the one matching node.
tap
¶
tap(timeout: float | None = None, *, count: int = 1, interval: float = 0.1) -> None
Tap count times (1..100) at the initially resolved visible centre.
interval is the release-to-press gap in seconds (0.04..1, default 0.1).
Each press lasts 50 ms. The whole sequence must fit in 30 seconds and the remaining
timeout, or no input is sent. Resolves once, never retries. Does not promise that an
app recognizes a multi-click gesture. Counted taps require the default display.
triple_tap
¶
triple_tap(timeout: float | None = None, *, interval: float = 0.1) -> None
Three taps with interval seconds between releases and presses; see :meth:tap.
long_tap
¶
long_tap(timeout: float | None = None, *, duration: float | None = None) -> None
Long press on one matching node.
Without duration, uses Android's default long-press timing. Otherwise holds a
physical finger for duration seconds (0.001..30); a short duration may not trigger
the app's long-click handler. Must fit in the remaining timeout before any input.
Explicit-duration presses require the default display. Tap never retries the input.
double_tap
¶
double_tap(timeout: float | None = None) -> None
Two taps at the centre of the one matching node's visible bounds, inside Android's double-tap window.
tap_outside_panel
¶
tap_outside_panel(timeout: float | None = None, *, allow_reach_below: bool = False) -> OutsideTouch
Tap outside the panel this element is in: a dialog, popup, menu, drawer or bottom sheet, Views or Compose. Select any node inside the panel, such as its title; it need not be tappable itself. This is how a test checks "a touch outside closes it".
The panel is the nearest node from this one up that offers Android's dismiss action or has a pane title (drawers and bottom sheets), and the touch goes beside it, where no control of the app's own would take it; otherwise it is the element's window (dialogs, popups, menus), and the touch goes just past its edge. Sides are tried below, above, right, left, skipping the system bars and gesture area, the keyboard and windows above.
Fails before any input with NOT_INTERACTABLE and detail NO_OUTSIDE_POINT when no
side has room (a full-screen window that is not a panel, such as a plain screen), or
TOUCH_REACHES_BELOW when the only free points are on the screen under a panel that
lets touches through (an autocomplete dropdown, a non-focusable popup): pass
allow_reach_below=True to send the touch there anyway. Whether the panel closes is the
app's choice: assert it, for example with app.wait(selector).gone(). Wait until the
panel has finished opening first: the point is placed from its bounds at that moment.
Default display only.
Returns where it touched and which panel it chose (:class:OutsideTouch).
ime_action
¶
ime_action(timeout: float | None = None) -> None
Run the one matching text field's keyboard action (Search, Go, Send, Done, … as the
app configured it) exactly as the keyboard's action key does, through accessibility
ACTION_IME_ENTER. API 30+: older devices raise CommandError (UNSUPPORTED /
REQUIRES_API_30) before any input, since pressing Enter is not equivalent. The field
must have input focus (tap it first): Android offers the action only then, and a node
that does not offer it raises ACTION_REJECTED before any input.
perform_action
¶
perform_action(action: StandardAction, timeout: float | None = None) -> None
Perform a standard accessibility action on the one matching node, as a screen
reader does: no touch, so a covered node is no obstacle. The node must offer it
(snapshot().actions); otherwise CommandError (ACTION_REJECTED /
ACTION_NOT_OFFERED) before any input. A node that refuses an offered action is
ACTION_REJECTED. Only Android's answer is reported: assert what the app did.
perform_custom_action
¶
perform_custom_action(label: str, timeout: float | None = None) -> None
Perform the custom accessibility action labelled label (exactly) on the one matching
node: the "Archive" or "Delete" a list item offers screen reader users instead of a swipe
(snapshot().custom_actions). Not offered, or offered twice under that label:
ACTION_REJECTED / ACTION_NOT_OFFERED before any input.
set_progress
¶
set_progress(value: float, timeout: float | None = None) -> None
Set the one matching range node (SeekBar, Slider, RatingBar) to value in its own
units (snapshot().range) through accessibility ACTION_SET_PROGRESS: exact, where
a drag would land on whatever pixel maps to. A node without the action, or a value
outside its min..max, is ACTION_REJECTED (ACTION_NOT_OFFERED / OUT_OF_RANGE)
before any input; the view is not left to clamp it.
drag_to
¶
drag_to(destination: Selector, timeout: float | None = None) -> None
Press the one matching node until it is a long press, move the finger to the centre of
the one node matching destination (resolved on the same screen, before any input),
hold there briefly and lift. Nothing is reported about what the app did with the drop.
pinch_open
¶
pinch_open(percent: int = DEFAULT_GESTURE_PERCENT, timeout: float | None = None) -> None
Two fingers moving apart from the one matching node's centre, across percent of its size.
pinch_close
¶
pinch_close(percent: int = DEFAULT_GESTURE_PERCENT, timeout: float | None = None) -> None
Two fingers moving together towards the one matching node's centre, across percent of its size.
fling
¶
fling(direction: Direction, timeout: float | None = None) -> None
One fast swipe across the one matching node towards direction's content edge (as for
scroll: DOWN flings towards content below). Returns once the finger lifts; the
content may keep moving, so wait for what you need next.
set_text
¶
set_text(value: str, timeout: float | None = None, *, sensitive: bool = False) -> None
Accessibility text replacement (ACTION_SET_TEXT) on the one matching node.
Fails with ACTION_REJECTED only when the node refuses the action. The field is not
read back: assert the effect with a selector that survives the edit, e.g.
app.wait(res("email")).text_equals("new").
With sensitive=True (a password, a token) the server logs the call without
value, so the connection's event log, tap-watcher and exports never hold it.
type_text
¶
type_text(value: str, await_focus: bool = True, timeout: float | None = None, *, sensitive: bool = False) -> None
Tap the one matching node, wait until it reports focus, then type value.
Three steps (tap, wait().focused() when await_focus, Device.type_text),
so a failure says which one failed. Pass await_focus=False when focus goes elsewhere
(a child or a separate input view) and wait for what that app needs yourself. The field
is not read back: assert the effect yourself. sensitive as for set_text.
clear_text
¶
clear_text(timeout: float | None = None) -> None
set_text(""): ACTION_SET_TEXT on the one matching node, not read back.
swipe
¶
swipe(direction: Direction, distance_percent: int = DEFAULT_GESTURE_PERCENT, timeout: float | None = None) -> None
One swipe gesture across the node in the direction the finger moves (UP/DOWN/LEFT/RIGHT).
scroll
¶
scroll(direction: Direction, distance_percent: int = DEFAULT_GESTURE_PERCENT, timeout: float | None = None) -> ScrollResult
Scroll towards the content edge (DOWN reveals content below).
Returns source-attributed movement/edge evidence when available; unknown does not mean unchanged or at end. Older drivers return unknown evidence.
scan
¶
scan(items: Selector, key: Callable[[ElementSnapshot], K], *, max_scrolls: int, max_items: int, timeout: float, direction: Direction = DOWN, distance_percent: int = 60, no_progress_limit: int = 2) -> ScanResult[K]
Collect bounded descendant observations from the current scroll position.
Explicit stable, non-null hashable keys merge overlap; duplicate keys in one read
fail. Distinct rows sharing keys across reads may be merged incorrectly. Stops at an
observed edge, a budget or repeated unchanged viewports, never claiming completeness.
Does not rewind or implicitly wait for loading. Read limits remain 1000 nodes / 1 MiB.
Failures keep their original type/code and carry scan_partial observations.
scroll_until
¶
scroll_until(target: Selector, direction: Direction = DOWN, max_scrolls: int = 20, distance_percent: int = DEFAULT_GESTURE_PERCENT, timeout: float | None = None) -> Element
Client-side loop: checks whether target exists inside this container
(descendant) and, while it does not, scrolls once, up to max_scrolls scrolls
within timeout seconds (default: the device's wait timeout). Returns the target as a
lazy element scoped to this container, so a later action cannot hit a duplicate
elsewhere on screen; a container with first()/at() cannot be carried into a
relation, and then the bare target is used.
Raises WaitTimeoutError when the target never appeared. The list may have scrolled
by then. Every step is an ordinary command, so a failing step raises its own
CommandError.
wait
¶
wait(timeout: float | None = None) -> ElementWait
An ElementWait on this selector (default timeout timeouts.wait).
descendant
¶
The node matching other somewhere below this one.
ElementWait
¶
Returned by Device.wait / Element.wait. Every wait but count polls on the device
in a single RPC; count polls from the host.
Methods:
| Name | Description |
|---|---|
visible |
Wait until at least one node matches; polled on the device in one RPC. A timeout's |
one |
Wait until exactly one node matches — what a mutation such as |
gone |
Wait until no node matches; polled on the device in one RPC. A timeout's |
until |
Wait until the one node the selector picks satisfies |
enabled |
|
disabled |
|
checked |
|
unchecked |
|
focused |
|
text_equals |
|
text_contains |
|
count |
Wait until exactly |
visible
¶
visible() -> Element
Wait until at least one node matches; polled on the device in one RPC. A timeout's
reason is NO_MATCH.
one
¶
one() -> Element
Wait until exactly one node matches — what a mutation such as tap() needs; polled
on the device in one RPC. A timeout's reason is NO_MATCH or AMBIGUOUS with
match_count from the last poll.
gone
¶
gone() -> None
Wait until no node matches; polled on the device in one RPC. A timeout's reason is
STILL_PRESENT with match_count.
until
¶
Wait until the one node the selector picks satisfies condition, then return it.
The selector says which node: exactly one match, or its first() / at(n). The
condition says what that node must become, written with the selector builders
(enabled() & text("Pay"), checked(False), text("Total", STARTS_WITH),
has_child(text("Done"))) and checked on that node alone, every poll, all parts at
the same moment. Polled on the device in one RPC. With several matches and no pick the
wait fails at once with AMBIGUOUS. A timeout's reason is STATE_NOT_REACHED
(last_state is the node at the end and condition_parts says which part failed) or
NO_MATCH. A condition with its own first() / at(n) raises ValueError.
text_equals
¶
text_equals(expected: str) -> Element
until(text(expected)): the raw accessibility text equals expected.
Elements
¶
A read-only selector bound to a device, from App.elements or Screen.elements.
Construction performs no I/O. Each read resolves all matches afresh; no node handles are
retained. App collections restrict matches to the app's package, screen collections do
not. Use selector relations such as res("list").descendant(res("title")) to restrict
matches to a container's descendants. first() / at() are rejected: use element
for singular reads. There is no implicit scrolling or iteration over live elements.
Methods:
| Name | Description |
|---|---|
snapshots |
Read all current matches with one device command, or |
texts |
Raw text of every match, projected from one :meth: |
snapshots
¶
snapshots(timeout: float | None = None) -> list[ElementSnapshot]
Read all current matches with one device command, or [] when none match.
Returns immutable value objects in accessibility order: service-reported window order, then post-order inside each window (children before their matching parent). This is not necessarily visual order. Matching/visibility rules are the same as singular element reads; virtualized lists expose only their current accessibility nodes, not the whole dataset.
Resolves one match set and reads it sequentially, not as an atomic UI transaction.
A stale lookup/read fails the whole operation with STALE_BEFORE_INPUT: no partial
results or internal retry. More than 1000 matches or a serialized snapshot list over
1 MiB raises CommandError (PAYLOAD_TOO_LARGE), never silent truncation.
timeout (seconds, default: the device action timeout) bounds the read; it does
not wait for matches or collection stability.
texts
¶
texts(timeout: float | None = None) -> list[str | None]
Raw text of every match, projected from one :meth:snapshots call.
Keeps order, duplicates, empty strings and None (no text); never filters entries.
On API 26+ an empty field may report its hint as text, as Element.text() does.
Use snapshots() to inspect showing_hint or multiple state properties together.
Bounded scanning¶
ScanResult
¶
Bases: Generic[K]
Detached observations in first-seen order, not guaranteed dataset order or completeness.
On an exception, exception.scan_partial contains this result with stop_reason=None.
The exception keeps its original type/code, including INDETERMINATE; never replay input.
scrolls
¶
scrolls: int
Scroll commands attempted, including a failing/indeterminate attempt in partial results.
CollectedObservation
¶
Bases: Generic[K]
One caller-defined identity, with first/latest values and number of reads containing it.
ScanStopReason
¶
ScrollResult
¶
Source-attributed accessibility evidence; absent evidence remains unknown.