#!/usr/bin/python3 import uiautomator2 as u2 import time def clear_all_apps(): d = u2.connect() while True: try: d.keyevent("HOME") d.keyevent("HOME") d.keyevent("MENU") time.sleep(1) if "com.miui.home:id/recents_container" not in d.dump_hierarchy(): continue if "近期没有任何内容" in d.dump_hierarchy(): d.keyevent("HOME") d.keyevent("HOME") exit(0) d.xpath('//*[@resource-id="com.miui.home:id/clearAnimView"]').click_exists(timeout=3) d.keyevent("HOME") d.keyevent("HOME") except Exception as e: print("clear_all_apps errors", e) d = u2.connect() continue if __name__=="__main__": clear_all_apps()