40 lines
1.0 KiB
Python
40 lines
1.0 KiB
Python
"""Click a UIA element by name inside a window (fallback: PostMessage click at center)."""
|
|
import sys, time
|
|
import uiautomation as auto
|
|
|
|
|
|
def main(hwnd, name):
|
|
win = auto.ControlFromHandle(hwnd)
|
|
if win is None:
|
|
print('no window for hwnd', hwnd)
|
|
return 1
|
|
target = None
|
|
for ctl in win.GetChildren():
|
|
if name in (ctl.Name or ''):
|
|
target = ctl
|
|
break
|
|
if target is None:
|
|
# search deeper
|
|
for ctl, depth in auto.WalkControl(win, maxDepth=6):
|
|
if name in (ctl.Name or ''):
|
|
target = ctl
|
|
break
|
|
if target is None:
|
|
print('not found:', name)
|
|
return 2
|
|
rect = target.BoundingRectangle
|
|
print('found:', target.ControlTypeName, repr(target.Name), rect)
|
|
try:
|
|
target.Click()
|
|
print('clicked via UIA')
|
|
except Exception as exc:
|
|
print('uia click failed:', exc)
|
|
return 3
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
hwnd = int(sys.argv[1])
|
|
name = sys.argv[2]
|
|
sys.exit(main(hwnd, name))
|