Automating Android Phones with Scrcpy and UiAutomator2 Using Python
This tutorial explains how to set up the open‑source screen‑copy tool Scrcpy and the Python‑based UiAutomator2 library to control an Android device from a computer, covering installation, environment configuration, sample code for interacting with apps, and practical use cases such as automated liking and commenting.
Hello, I'm Jack. In a recent video I demonstrated code‑driven phone automation, and this article details the steps I used.
Scrcpy (screen copy) is an open‑source, cross‑platform tool that mirrors an Android device’s screen to a PC over USB or Wi‑Fi. It works on Linux, Windows, and macOS. The project is available at https://github.com/Genymobile/scrcpy .
On Windows, install Scrcpy by downloading the zip archive, extracting it, and adding the folder (which includes adb ) to the system PATH.
Connect the phone via USB, enable Developer Options , then run the command:
scrcpyThis launches a window showing the phone screen, allowing you to control the device from the PC.
For programmatic control, use UiAutomator2 , a Python wrapper around Google’s UiAutomator framework. Unlike the original Java‑only UiAutomator, UiAutomator2 lets you write automation scripts in Python.
The project is hosted at https://github.com/openatx/uiautomator2 . Install it in a Conda environment:
conda create -n android
conda activate android
python -m pip install uiautomator2 weditorLaunch the web inspector with:
python -m weditorUse the inspector to locate UI elements (e.g., the like button in BiliBili) and then automate actions such as liking, coin‑paying, and following via Python code. A representative script is:
import uiautomator2 as u2
import time
from PIL import Image
import cv2
import numpy as np
all_videos = []
def get_images(device):
views = device.xpath('//*[@resource-id="tv.danmaku.bili:id/recycler_view"]/android.view.ViewGroup/android.widget.FrameLayout[1]')
for idx, view_box in enumerate(views.all()[:5]):
print("视频{} 封面的中心坐标:".format(idx+1), view_box.center())
image = view_box.screenshot()
image.save("{}.jpg".format(idx+1))
all_videos.append(view_box)
def refresh(device):
device.swipe_ext("down")
def like_the_video(device):
like_icon = device.xpath('//*[@resource-id="tv.danmaku.bili:id/recommend_icon"]')
like_icon.click()
print("视频点赞成功")
def pay_for_the_video(device):
coin_icon = device.xpath('//*[@resource-id="tv.danmaku.bili:id/coin_icon"]')
coin_icon.click()
time.sleep(0.1)
pay_icon = device.xpath('//*[@resource-id="tv.danmaku.bili:id/pay_coins"]')
pay_icon.click()
print("视频投币成功")
def follow_the_up(device):
follow_icon = device.xpath('//*[@resource-id="tv.danmaku.bili:id/follow"]')
follow_icon.click()
print("关注成功")
def back(device):
back_icon = device.xpath('//*[@content-desc="转到上一层级"]')
back_icon.click()
print("已退出视频")
if __name__ == "__main__":
_DEVICE_ID = 'da317199'
d = u2.connect(_DEVICE_ID) # connect to device
get_images(d)
all_videos[3].click()
print("点进去了!")
time.sleep(0.1)
like_the_video(d)
time.sleep(0.1)
pay_for_the_video(d)
time.sleep(0.1)
follow_the_up(d)Find the device ID with adb devices after connecting the phone via USB.
This approach can be extended to automate tasks that require a mobile device, such as rapid ticket purchases or other time‑critical actions.
Bottom line: Don’t discard old phones; with a few scripts they become powerful automation tools.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.