Python subprocess 使用(二)
本篇继续介绍subprocess的使用.
这里主要添加两个自己在工作过程中常用的两个小命令.
1: 获取顶层activity
import subprocess
def get_top_activity():
# 使用adb命令获取顶层activity
cmd = 'adb shell dumpsys window |grep "mCurrentFocus=Window"'
# 解析输出结果,提取顶层activity
result = subprocess.check_output(cmd, shell=True).decode('utf-8').strip()
return result
# 调用函数获取顶层activity
print("Top activity:", get_top_activity())
2: 获取top activity 某个view 的id
import subprocess
import sys,os
name = sys.argv[0]
arg =sys.argv[1]
def get_control_id():
# 使用adb命令获取当前activity某个组件id
cmd = 'adb shell dumpsys activity top |grep "app:id/'+arg+'"'
print(cmd)
result = subprocess.check_output(cmd, shell=True).decode('utf-8').strip()
return result
# 调用函数获取顶层activity
print("id/"+arg+":", get_control_id())
参数传入目标view的id name:
python3 topActivity.py item_icon
输出如下:
Top activity: mCurrentFocus=Window{bbad0c0 u0 com.test.signapk/com.test.signapk.MainActivity}
adb shell dumpsys activity top |grep "app:id/item_icon"
id/item_icon: androidx.appcompat.widget.AppCompatImageView{c12b536 V.ED..... ........ 35,35-315,315 #7f08008c app:id/item_icon}
androidx.appcompat.widget.AppCompatImageView{1f96f09 V.ED..... ........ 35,35-315,315 #7f08008c app:id/item_icon}
androidx.appcompat.widget.AppCompatImageView{9342b4b V.ED..... ........ 35,35-315,315 #7f08008c app:id/item_icon}
androidx.appcompat.widget.AppCompatImageView{607b57d V.ED..... ........ 35,35-315,315 #7f08008c app:id/item_icon}
androidx.appcompat.widget.AppCompatImageView{e8c756c V.ED..... ........ 35,35-315,315 #7f08008c app:id/item_icon}
androidx.appcompat.widget.AppCompatImageView{8f2f622 V.ED..... ........ 35,35-315,315 #7f08008c app:id/item_icon}
androidx.appcompat.widget.AppCompatImageView{c4337a5 V.ED..... ........ 35,35-315,315 #7f08008c app:id/item_icon}
标签:Python,activity,35,subprocess,item,315,使用,id,icon
From: https://www.cnblogs.com/zhjing/p/17929259.html