Element Selection
Selecting the Nth Element on Screen Left to Right
This example shows how you can find multiple elements on the screen using a reference image and click on a specific one based on its position from the left to the right.
Example Code
What This Does
1. Waits for UI to load
Before doing anything, the test waits for one second to make sure the UI is rendered and stable.
2. Finds all matching elements
We search the screen for all elements that match the given reference image:
This returns a list of matching elements.
3. Sorts elements top to bottom
We sort the found elements by their horizontal position on the screen (xmin
):
This ensures the topmost element is at index 0
, the second one at index 1
, and so on.
4. Clicks on the selected element
We pick the element at the desired index, move the mouse to its center, and click:
When to Use This
Use this approach if:
- You want to interact with repeated elements (e.g., list items, buttons).
- You need to click a specific one (e.g., the second, third, etc.).
- The elements look identical or similar, and position is the only differentiator.
Tips
- Make sure the reference image clearly captures the element.
- Test with different screen resolutions or zoom settings to ensure consistency.
- You can adjust the
index
parameter to click a different element.