Android Uiautomator2 Python Wrapper
A simple, easy-to-use, and stable Android automation library.
Users still on version 2.x.x, please check 2to3 before deciding to upgrade to 3.x.x (Upgrade is highly recommended).
This framework mainly consists of two parts:
Simply put, it exposes Android automation capabilities to Python through HTTP interfaces. This design makes Python-side code writing simpler and more intuitive.
The Device Side run with a jar file which is also open source. see https://github.com/openatx/android-uiautomator-server-jar
pip install uiautomator2
# Check if installation was successful, normally it will output the library version
uiautomator2 version
# or: python -m uiautomator2 versionInstall element inspection tool (optional, but highly recommended):
For more detailed usage instructions, refer to: https://github.com/uiautodev/uiautodev QQ:536481989
# Recommended: launch via npx (requires Node.js), it will automatically open the browser
npx uiautodev -open
# If Node.js is not installed, download the standalone binary for your platform
# from https://get.uiauto.dev and run it directly.
# Alternative: install the Python version via pip
pip install uiautodev
uiautodev
# or: python -m uiautodevAlternatives: uiautomatorviewer, Appium Inspector
Prepare an Android phone with Developer options enabled, connect it to the computer, and ensure that adb devices shows the connected device.
Open a Python interactive window. Then, input the following commands into the window.
import uiautomator2 as u2
d = u2.connect() # Specify device serial number if multiple devices are connected
print(d.info)
# Expected output
# {'currentPackageName': 'net.oneplus.launcher', 'displayHeight': 1920, 'displayRotation': 0, 'displaySizeDpX': 411, 'displaySizeDpY': 731, 'displayWidth': 1080, 'productName': 'OnePlus5', 'screenOn': True, 'sdkInt': 27, 'naturalOrientation': True}Example script:
import uiautomator2 as u2
d = u2.connect('Q5S5T19611004599')
d.app_start('tv.danmaku.bili', stop=True) # Start Bilibili
d.wait_activity('.MainActivityV2')
d.sleep(5) # Wait for splash screen ad to disappear
d.xpath('//*[@text="我的"]').click() # Click "My"
# Get fan count
fans_count = d.xpath('//*[@resource-id="tv.danmaku.bili:id/fans_count"]').text
print(f"Fan count: {fans_count}")Method 1: Connect using device serial number, e.g., Q5S5T19611004599 (seen from adb devices)
import uiautomator2 as u2
d = u2.connect('Q5S5T19611004599') # alias for u2.connect_usb('123456f')
print(d.info)To connect on a non-default port (default is 9008):
d = u2.connect('Q5S5T19611004599', port=9009)Method 2: Serial number can be passed via environment variable ANDROID_SERIAL
# export ANDROID_SERIAL=Q5S5T19611004599
d = u2.connect()Method 3: Specify device via transport_id
$ adb devices -l
Q5S5T19611004599 device 0-1.2.2 product:ELE-AL00 model:ELE_AL00 device:HWELE transport_id:6Here you can see transport_id:6.
You can also get all connected transport_ids via
adbutils.adb.list(extended=True)Refer to https://github.com/openatx/adbutils
import adbutils # Requires version >=2.9.1
import uiautomator2 as u2
dev = adbutils.device(transport_id=6)
d = u2.connect(dev)What is XPath:
XPath is a query language for locating content in XML or HTML documents. It uses simple syntax rules to establish a path from the root node to the desired element.
Basic Syntax:
/ - Select from the root node// - Select from any position starting from the current node. - Select the current node.. - Select the parent of the current node@ - Select attributes[] - Predicate expression, used for filtering conditionsYou can quickly generate XPath using UIAutoDev.
Common Usage:
…For more usage, refer to XPath Interface Document
To maintain the project's simplicity and extensibility, future plugins will be integrated as third-party libraries.
Set element search wait time (default 20s)
d.implicitly_wait(10.0) # Can also be modified via d.settings['wait_timeout'] = 10.0
print("wait timeout", d.implicitly_wait()) # get default implicit wait
# Throws UiObjectNotFoundError if "Settings" does not appear in 10s
d(text="Settings").click()Wait timeout affects the following functions: click, long_click, drag_to, get_text, set_text, clear_text.
Information obtained via UiAutomator:
d.info
# Output
{'currentPackageName': 'com.android.systemui',
'displayHeight': 1560,
'displayRotation': 0,
'displaySizeDpX': 360,
'displaySizeDpY': 780,
'displayWidth': 720,
'naturalOrientation': True,
'productName': 'ELE-AL00',
'screenOn': True,
'sdkInt': 29}Get device information (based on adb shell getprop command):
print(d.device_info)
# output
{'arch': 'arm64-v8a',
'brand': 'google',
'model': 'sdk_gphone64_arm64',
'sdk': 34,
'serial': 'EMULATOR34X1X19X0',
'version': 14}Get screen physical size (depends on adb shell wm size):
print(d.window_size())
# device upright output example: (1080, 1920)
# device horizontal output example: (1920, 1080)Get current App (depends on adb shell):
print(d.app_current())
# Output example 1: {'activity': '.Client', 'package': 'com.netease.example', 'pid': 23710}
# Output example 2: {'activity': '.Client', 'package': 'com.netease.example'}
# Output example 3: {'activity': None, 'package': None}Wait for Activity (depends on adb shell):
d.wait_activity(".ApiDemos", timeout=10) # default timeout 10.0 seconds
# Output: true or falseGet device serial number:
print(d.serial)
# output example: 74aAEDR428Z9Get device WLAN IP (depends on adb shell):
print(d.wlan_ip)
# output example: 10.0.0.1 or NoneSet or get clipboard content.
clipboard/set_clipboard
# Set clipboard
d.clipboard = 'hello-world'
# or
d.set_clipboard('hello-world', 'label')
# Get clipboard
# Depends on input method (com.github.uiautomator/.AdbKeyboard)
d.set_input_ime()
print(d.clipboard)Turn on/off screen
d.screen_on() # turn on the screen
d.screen_off() # turn off the screenGet current screen status
d.info.get('screenOn')Press hard/soft key
d.press("home") # press the home key, with key name
d.press("back") # press the back key, with key name
d.press(0x07, 0x02) # press keycode 0x07('0') with META ALT(0x02)These key names are currently supported:
You can find all key code definitions at Android KeyEvent
Unlock screen
d.unlock()
# This is equivalent to
# 1. press("power")
# 2. swipe from left-bottom to right-topClick on the screen
d.click(x, y)Double click
d.double_click(x, y)
d.double_click(x, y, 0.1) # default duration between two clicks is 0.1sLong click on the screen
d.long_click(x, y)
d.long_click(x, y, 0.5) # long click 0.5s (default)Swipe
d.swipe(sx, sy, ex, ey)
d.swipe(sx, sy, ex, ey, 0.5) # swipe for 0.5s (default)SwipeExt (Extended functionality)
…Drag
d.drag(sx, sy, ex, ey)
d.drag(sx, sy, ex, ey, 0.5) # drag for 0.5s (default)Swipe points
# swipe from point(x0, y0) to point(x1, y1) then to point(x2, y2)
# time will be 0.2s between two points
d.swipe_points([(x0, y0), (x1, y1), (x2, y2)], 0.2)Often used for pattern unlock, get relative coordinates of each point beforehand (supports percentages). For more detailed usage, refer to this post Using u2 to implement pattern unlock
Touch and drag (Beta)
This is a lower-level raw interface, feels incomplete but usable. Note: percentages are not supported here.
d.touch.down(10, 10) # Simulate press down
time.sleep(.01) # Delay between down and move, control it yourself
d.touch.move(15, 15) # Simulate move
d.touch.up(10, 10) # Simulate releaseNote: click, swipe, drag operations support percentage position values. Example:
d.long_click(0.5, 0.5) means long click center of screen.
Retrieve/Set device orientation
The possible orientations:
natural or nleft or lright or rupsidedown or u (cannot be set)# retrieve orientation. the output could be "natural" or "left" or "right" or "upsidedown"
orientation = d.orientation
# WARNING: did not pass testing on my TT-M1
# set orientation and freeze rotation.
# notes: setting "upsidedown" requires Android>=4.3.
d.set_orientation('l') # or "left"
d.set_orientation("r") # or "right"
d.set_orientation("n") # or "natural"Freeze/Un-freeze rotation
# freeze rotation
d.freeze_rotation()
# un-freeze rotation
d.freeze_rotation(False)Take screenshot
…Dump UI hierarchy
# get the UI hierarchy dump content
xml = d.dump_hierarchy()
# compressed=True: include non-important nodes (default False)
# pretty: format xml (default False)
# max_depth: limit xml depth, default 50
xml = d.dump_hierarchy(compressed=False, pretty=True, max_depth=30)
# root_in_active=True: dump only the active window root
xml = d.dump_hierarchy(root_in_active=True)Open notification or quick settings
d.open_notification()
d.open_quick_settings()Show touch trace on device screen
# show touch trace on device screen
d.show_touch_trace()
# hide touch trace
d.show_touch_trace(pointer_location=False, show_touches=False)Selector is a handy mechanism to identify a specific UI object in the current window.
# Select the object with text 'Clock' and its className is 'android.widget.TextView'
d(text='Clock', className='android.widget.TextView')Selector supports the below parameters. Refer to UiSelector Java doc for detailed information.
text, textContains, textMatches, textStartsWithclassName, classNameMatchesdescription, descriptionContains, descriptionMatches, descriptionStartsWithcheckable, checked, clickable, longClickablescrollable, enabled,focusable, focused, selectedpackageName, packageNameMatchesresourceId, resourceIdMatchesindex, `inst[Proposal] Add u2_flutter - Flutter driver plugin for uiautomator2
package_version raises InvalidVersion when APK versionName is null or empty
多屏幕支持
请问有没有计划支持类似playwright-cli那样的uiautomator2-cli,然后开放skills给ai使用呢?
3.5.1版本偶现报错'StackOverflowError: java.lang.StackOverflowError'
uiautodev无法获取抖音热榜信息
add supoort for muilty screen sush as add displayId parmeter for operate,and find_element( displayId,text)
程序未按照设置的等待时间停止!!!!
频繁使用xpath中的方法会导致手机端uiautomator jar包起的服务出现内存泄漏
get parent obj of uielement