源码:
def get_message(self,
wait_timeout=10,
cache_timeout=10,
default=None):
"""
Args:
wait_timeout: seconds of max wait time if toast now show right now
cache_timeout: return immediately if toast showed in recent $cache_timeout
default: default messsage to return when no toast show up
Returns:
None or toast message
"""
自动获取toast,当有toast时,截图保存并语音播报:
import uiautomator2 as u2
import win32com.client
from datetime import datetime
d = u2.connect()
while True:
toast = d.toast.get_message(wait_timeout=5)
if toast:
print(toast) # 打印toast信息
d.screenshot('./IMG/%stoast.jpg' % datetime.now().strftime('%Y%m%d%H%M%S')) # 截图保存
speaker = win32com.client.Dispatch("SAPI.SpVoice") # 语音播放获取到的toast信息
speaker.Speak(toast)
d.toast.reset() # 在验证成功后及时清除缓存