#!/usr/bin/env python3 """tap.py TEXT: tap the first on-screen element whose text or description contains TEXT.""" import re, subprocess, sys, os adb = os.path.expanduser("~/Android/Sdk/platform-tools/adb") xml = subprocess.run([adb, "exec-out", "uiautomator", "dump", "/dev/tty"], capture_output=True, text=True).stdout want = sys.argv[1] exact = want.startswith("=") if exact: want = want[1:] for m in re.finditer(r']*>', xml): n = m.group(0) t = (re.search(r' text="([^"]*)"', n) or [None, ""])[1] d = (re.search(r'content-desc="([^"]*)"', n) or [None, ""])[1] if not re.search(r'bounds=', n): continue if (t == want or d == want) if exact else (want in t or want in d): x1, y1, x2, y2 = map(int, re.search(r'bounds="\[(\d+),(\d+)\]\[(\d+),(\d+)\]"', n).groups()) subprocess.run([adb, "shell", "input", "tap", str((x1 + x2) // 2), str((y1 + y2) // 2)]) print("tapped", t or d, (x1 + x2) // 2, (y1 + y2) // 2) sys.exit(0) print("not found:", want); sys.exit(1)