find_element_by_id
Locate Android UI elements by resource ID to obtain element details and tap coordinates for testing and debugging purposes.
Instructions
Find a UI element by its resource ID. Returns element details including tap coordinates.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| resource_id | Yes | ||
| device_serial | No |
Implementation Reference
- src/adb_mcp_server/server.py:354-387 (handler)The primary handler function for the 'find_element_by_id' tool. It is registered via the @mcp.tool() decorator. Dumps current UI hierarchy XML, parses nodes to find matching resource ID (supports partial matching), extracts relevant properties like text and bounds, computes tap center, and returns element dict or None.@mcp.tool() def find_element_by_id( resource_id: str, device_serial: str | None = None ) -> dict | None: """ Find a UI element by its resource ID. Returns element details including tap coordinates. """ xml = get_ui_hierarchy(device_serial) # Match partial resource ID (e.g., "button_submit" matches "com.app:id/button_submit") for match in re.finditer(r'<node[^>]*>', xml): node = match.group() id_match = re.search(r'resource-id="([^"]*)"', node) if id_match and resource_id in id_match.group(1): element = {'resource_id': id_match.group(1)} text_match = re.search(r'text="([^"]*)"', node) if text_match: element['text'] = text_match.group(1) bounds_match = re.search(r'bounds="\[(\d+),(\d+)\]\[(\d+),(\d+)\]"', node) if bounds_match: x1, y1 = int(bounds_match.group(1)), int(bounds_match.group(2)) x2, y2 = int(bounds_match.group(3)), int(bounds_match.group(4)) element['center'] = {'x': (x1 + x2) // 2, 'y': (y1 + y2) // 2} element['bounds'] = {'x1': x1, 'y1': y1, 'x2': x2, 'y2': y2} return element return None
- src/adb_mcp_server/server.py:354-354 (registration)The @mcp.tool() decorator registers the find_element_by_id function as an MCP tool.@mcp.tool()
- src/adb_mcp_server/server.py:416-416 (helper)Usage of find_element_by_id within the tap_element tool.element = find_element_by_id(resource_id, device_serial=device_serial)