Python subprocess 使用(二)

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}

  • 13
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值