Using the ADB command in Python, you can import an OS module.
1 simple adb order.
such as: Os.system (' adb version ')
2 A somewhat complicated adb order.
such as: Os.system (' adb shell ' dumpsys activity | grep "Mfocusedactivity" "")
3 want to save the contents of the ADB read out.
such as: out = Os.popen (the ' adb shell ' dumpsys activity | grep "mfocusedactivity" "'). Read ()
To sum up: Os.system does not support reading, Os.popen supports reading.
Here's a transfer script to learn:
Import OS
import re
os.system (' adb version ')
os.system (' adb devices ') #os. System is out of read operation not supported
Os.popen (' adb shell ' dumpsys activity | grep "mfocusedactivity" "'). Read () #os. Popen supports read operations
print (out)
#下面的代码是获取当前窗口的component参数
def Getfocusedpackageandactivity (): Pattern
= Re.compile (r "[A-za-z0-9\.] +/[a-za-z0-9\.] + ") #这里使用了正则表达式, the content of the output is restricted, only the string out like" Com.mediatek.factorymode/com.mediatek.factorymode.factorymode "is displayed
Os.popen ("ADB shell dumpsys window Windows | Findstr \/| Findstr name= "). Read () #window下使用findstr
list = Pattern.findall (out)
component = List[0] #输出列表中的第一条字符串
Return component
Print (Getfocusedpackageandactivity ())