环境:
在手机中依次执行(点击):设置->我的设备->全部参数->MIUI版本(多次点击,开启开发者模式)->返回设置列表->更多设置->开发者选项->开启开发者选项并分别开启:
USB调试、USB安装、USB调试(安全设置)选项
此时手机上会弹出USB的用途弹框,我们选择传输文件(MTP)即可
在电脑 CMD 中执行 adb devices
命令,看是否能找到自己的手机,比如下图所示就是成功的结果了
实现
import time from appium import webdriver """ 找出了删除我微信的所有人并将他们自动化删除了 """ def is_element_exist(element, timeout=3): """ 判断元素是否存在 """ count = 0 while count < timeout: souce = driver.page_source if element in souce: return True else: count += 1 time.sleep(1) return False desired_caps = { "platformName": "Android", # 系统 "deviceName": "308f9a91", # 设备 ID "platformVersion": "10", # 设备版本号 "appPackage": "com.tencent.mm", # 包名 "appActivity": ".ui.LauncherUI", # app 启动时主 Activity 'unicodeKeyboard': True, # 使用自带输入法 'noReset': True # 保留 session 信息,可以避免重新登录 } def swipe_up(distance, times): """ 上拉方法 :param distance: 为滑动距离 :param times: 为滑动时间 """ width = 1080 height = 1920 # width 和 height根据不同手机而定 driver.swipe(1 / 2 * width, 9 / 10 * height, 1 / 2 * width, (9 / 10 - distance) * height, times) def get_address_list(flag): """ 获取通讯录列表 """ if flag: driver.find_elements_by_id('com.tencent.mm:id/cn_')[1].click() swipe_up(1 / 2, 2000) else: swipe_up(5 / 6, 2000) # 获取昵称(备注) address_list = driver.find_elements_by_id('com.tencent.mm:id/dy5') remark_list = [] for address in address_list: remark = address.get_attribute("content-desc") # 排除自己和微信官方号 if remark != "文件传输助手" and "微信团队" and "自己微信名称" not in remark: remark_list.append(remark) return remark_list def is_delete(remark, count): """ 判断是否删除 """ if count == "1": driver.find_element_by_id('com.tencent.mm:id/cn1').click() # 点击微信搜索框 time.sleep(1) driver.find_element_by_id('com.tencent.mm:id/bhn').send_keys(remark) # 在搜索框输入搜索信息 time.sleep(1) driver.find_element_by_id('com.tencent.mm:id/tm').click() # 点击搜索到的好友 time.sleep(1) driver.find_element_by_id('com.tencent.mm:id/aks').click() # 点击更多+号 time.sleep(1) driver.find_elements_by_id('com.tencent.mm:id/pa')[5].click() # 点击转账 time.sleep(1) driver.find_element_by_id('com.tencent.mm:id/cx_').click() # 点击键盘1 time.sleep(1) driver.find_element_by_id('com.tencent.mm:id/cxi').click() # 点击转账 time.sleep(1) # 判断是否被删以及是否为风险提醒用户 is_exist = is_element_exist('com.tencent.mm:id/jh') is_exist_risk = is_element_exist('取消支付') if is_exist: return remark elif is_exist_risk: driver.find_element_by_accessibility_id('取消支付').click() # 点击取消支付 return False else: return False def search_back(): """ 返回搜索框 """ driver.find_element_by_id('com.tencent.mm:id/dn').click() # 点击转账页面返回按钮 time.sleep(1) driver.find_element_by_id('com.tencent.mm:id/rs').click() # 点击联系人返回按钮 time.sleep(1) driver.find_element_by_id('com.tencent.mm:id/fsv').click() # 清除搜索框,输入下一个 def del_person(nicks): """ 删除把自己删除的人""" for index, value in enumerate(nicks): time.sleep(1) if index == 0: driver.find_element_by_id('com.tencent.mm:id/bhn').send_keys(value) # 在搜索框输入搜索信息 else: time.sleep(1) driver.find_element_by_id('com.tencent.mm:id/cn1').click() # 点击微信搜索框 time.sleep(1) driver.find_element_by_id('com.tencent.mm:id/bhn').send_keys(value) # 在搜索框输入搜索信息 time.sleep(1) driver.find_element_by_id('com.tencent.mm:id/tm').click() # 点击搜索到的人 time.sleep(1) driver.find_element_by_id('com.tencent.mm:id/cj').click() # 点击聊天对话框右上角.. time.sleep(1) driver.find_element_by_id('com.tencent.mm:id/f3y').click() # 点击头像 time.sleep(1) driver.find_element_by_id('com.tencent.mm:id/cj').click() # 点击联系人右上角... time.sleep(1) driver.find_element_by_id('com.tencent.mm:id/g6f').click() # 点击删除按钮 time.sleep(1) driver.find_element_by_id('com.tencent.mm:id/doz').click() # 点击弹出框中的删除 if __name__ == '__main__': remarks = [] driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps) time.sleep(2) remarks1 = get_address_list(True) remarks.extend(remarks1) while True: # 是否到了通讯录末尾 is_end = is_element_exist('com.tencent.mm:id/azb') time.sleep(1) remarks2 = get_address_list(False) remarks.extend(remarks2) if is_end: break remarks = set(remarks) # 去重 print("通讯录昵称列表总数:", len(remarks)) print("通讯录昵称列表:", remarks) time.sleep(3) dels = [] for inx, val in enumerate(remarks): rt = "" if inx == 0: rt = is_delete(val, "1") else: rt = is_delete(val, "") if rt is False: driver.keyevent(4) search_back() # 被删除 else: dels.append(rt) time.sleep(1) driver.find_element_by_id('com.tencent.mm:id/doz').click() # 点击提示框的“知道了” search_back() print("删除我的人总数:", len(dels)) print("删除我的人:", dels) del_person(dels) # 删除-删除我的人
参考地址:https://mp.weixin.qq.com/s/n9XD5PNIghvGM84ghqvkLA