Skip to main content

Automate Android Screenshots and Swipes with Python ADB

· One min read
Apache Wangye
Software developer and technical writer

This example uses ADB from Python to capture a screenshot from an Android device, copy it to the computer, and perform a swipe gesture.

Use ADB automation only on devices and applications you own or are authorized to test. Respect the target platform's rules, verify that the intended device is connected, and add delays and stop conditions so the loop cannot run out of control.

Install the dependency

pip install adbutils==2.7.2

Enable USB debugging, authorize the computer, and verify the connection:

adb devices

Example

import time
from datetime import datetime

import adbutils


def main() -> None:
devices = adbutils.adb.device_list()
if not devices:
raise RuntimeError("No authorized Android device is connected")

device = devices[0]

try:
while True:
filename = datetime.now().strftime("%Y%m%d%H%M%S") + ".png"
remote_path = "/sdcard/DCIM/screenshot.png"

device.shell(f"screencap -p {remote_path}", encoding=None)
device.sync.pull_file(remote_path, filename)
print(f"screenshot: {filename}")

device.shell("input swipe 760 1600 760 800 200")
time.sleep(2)
except KeyboardInterrupt:
print("Stopped")


if __name__ == "__main__":
main()

The swipe coordinates depend on the screen resolution and orientation. For a reusable script, query the display size first, calculate coordinates proportionally, check command results, and use a bounded loop instead of while True.

Page views: --

Total views -- · Visitors --