Python安卓自动化

参考网址:https://zhuanlan.zhihu.com/p/746925640

  • 踩坑记录
- 安装appium要注意的点
	
    # 切换到Node 18.13.0
    nvm use 18.13.0

    # 安装Appium 2.x(最新稳定版)
    npm install -g appium@next
  • 驱动安装信息
C:\Users\wcw.ACLAS>appium
[Appium] Welcome to Appium v2.0.1
[Appium] Appium REST http interface listener started on http://0.0.0.0:4723
[Appium] You can provide the following URLS in your client code to connect to this server:
[Appium]        http://192.168.11.38:4723/
[Appium]        http://192.168.56.1:4723/
[Appium]        http://192.168.18.1:4723/
[Appium]        http://192.168.91.1:4723/
[Appium]        http://127.0.0.1:4723/ (only accessible from the same host)
[Appium] No drivers have been installed in C:\Users\wcw.ACLAS\.appium. Use the "appium driver" command to install the one(s) you want to use.
[Appium] No plugins have been installed. Use the "appium plugin" command to install the one(s) you want to use.
[Appium] Received SIGINT - shutting down
[debug] [AppiumDriver@308b] There are no active sessions for cleanup
[HTTP] Closing Appium HTTP server
[HTTP] Appium HTTP server has been successfully closed after 1ms




# 这里要安装对的版本,否则后面会出错: npm view appium-uiautomator2-driver versions --json
C:\Users\wcw.ACLAS>appium driver install uiautomator2@2.45.1
√ Installing 'uiautomator2' using NPM install spec 'appium-uiautomator2-driver'
i Driver uiautomator2@6.1.1 successfully installed
- automationName: UiAutomator2
- platformNames: ["Android"]
  • 常用命令
adb devices
adb connect 127.0.0.1:16384
# 安卓内核版本号(先列出所有的设备,再指定设备,查看版本号)
C:\Users\wcw.ACLAS>adb devices
List of devices attached
127.0.0.1:16384 device
emulator-5554   device
C:\Users\wcw.ACLAS>adb -s emulator-5554 shell getprop ro.build.version.release
12

# 查看deviceName
C:\Users\wcw.ACLAS>adb devices -l
List of devices attached
127.0.0.1:16384        device product:a55x model:SM_A5560 device:a55x transport_id:2
emulator-5554          device product:a55x model:SM_A5560 device:a55x transport_id:1

# 使用第一个设备 (127.0.0.1:16384)
desired_caps = {
    'platformName': 'Android',
    'deviceName': '127.0.0.1:16384',  # 直接使用设备序列号
    'udid': '127.0.0.1:16384',  # 明确指定设备ID
    # 其他配置...
}

# 或者使用第二个设备 (emulator-5554)
desired_caps = {
    'platformName': 'Android',
    'deviceName': 'emulator-5554',
    'udid': 'emulator-5554',
    # 其他配置...
}

### 查看appPackage

# 使用 127.0.0.1:16384 设备
adb -s 127.0.0.1:16384 shell dumpsys activity | findstr "mFocus"

# 或者使用 emulator-5554 设备
adb -s emulator-5554 shell dumpsys activity | findstr "mFocus"
C:\Users\wcw.ACLAS>adb -s 127.0.0.1:16384 shell dumpsys activity | findstr "mFocus"
  mFocusedApp=ActivityRecord{8c325a5 u0 cn.yonghui.hyd/.MainActivity t12}
    mFocusedWindow=Window{171df55 u0 cn.yonghui.hyd/cn.yonghui.hyd.MainActivity}

代码中的capabilities该怎么填写

import unittest

from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy

capabilities = dict(
    platformName='Android',
    automationName='uiautomator2',
    deviceName='127.0.0.1:16384',
    # appPackage='com.tencent.mm',
    # appActivity='.ui.LauncherUI',
    # language='en',
    # locale='US'
)
posted @ 2025-11-10 16:17  清安宁  阅读(13)  评论(0)    收藏  举报