Skip to content

Instantly share code, notes, and snippets.

@Kardelio
Created April 7, 2023 11:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kardelio/4843006329ecf1ea3d66f94c5028c268 to your computer and use it in GitHub Desktop.
Save Kardelio/4843006329ecf1ea3d66f94c5028c268 to your computer and use it in GitHub Desktop.
Bash script to trigger python uiautomator2 to get specific UI element on android screen and click it if it exists
#!/usr/bin/env bash
#NOTE: requires python3 and the pip package uiautomator2
#These functions can be copied over to your own bash script
#and can be used by simply triggering tapIfExists...
function tapIfExists(){
coords=$(getCoords "${1}")
if [[ "$coords" != "-" ]]; then
$fullAdb shell input tap "$coords"
fi
}
function getCoords(){
fastdump=$(/opt/homebrew/bin/python3 -c "import uiautomator2 as u2;d=u2.connect(); out=d.dump_hierarchy(); print(out)")
present=$(echo "$fastdump" | grep "text\=\"${1}\"")
if [[ $? -eq 0 ]]; then
arr=($(echo "$fastdump" | sed 's/></>\n</g' | grep "text\=\"${1}\"" | grep -o "\[.*\]" | sed 's/\]\[/,/g' | sed 's/\]//g;s/\[//g;s/,/\n/g'))
x=$(echo "(${arr[0]}+${arr[2]})/2" | bc)
y=$(echo "(${arr[1]}+${arr[3]})/2" | bc)
echo "$x $y"
else
echo "-"
fi
}
tapIfExists "Log in"
sleep 1
tapIfExists "Maybe later"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment