importtimefrom appium importwebdriver"""找出了删除我微信的所有人并将他们自动化删除了"""
def is_element_exist(element, timeout=3):"""判断元素是否存在"""count=0while count
souce=driver.page_sourceif element insouce:returnTrueelse:
count+= 1time.sleep(1)returnFalse
desired_caps={"platformName": "Android", #系统
"deviceName": "308f9a91", #设备 ID
"platformVersion": "10", #设备版本号
"appPackage": "com.tencent.mm", #包名
"appActivity": ".ui.LauncherUI", #app 启动时主 Activity
'unicodeKeyboard': True, #使用自带输入法
'noReset': True #保留 session 信息,可以避免重新登录
}defswipe_up(distance, times):"""上拉方法
:param distance: 为滑动距离
:param times: 为滑动时间"""width= 1080height= 1920 #width 和 height根据不同手机而定
driver.swipe(1 / 2 * width, 9 / 10 * height, 1 / 2 * width, (9 / 10 - distance) *height, times)defget_address_list(flag):"""获取通讯录列表"""
ifflag:
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 inaddress_list:
remark= address.get_attribute("content-desc")#排除自己和微信官方号
if remark != "文件传输助手" and "微信团队" and "自己微信名称" not inremark:
remark_list.append(remark)returnremark_listdefis_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('取消支付')ifis_exist:returnremarkelifis_exist_risk:
driver.find_element_by_accessibility_id('取消支付').click() #点击取消支付
returnFalseelse:returnFalsedefsearch_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() #清除搜索框,输入下一个
defdel_person(nicks):"""删除把自己删除的人"""
for index, value inenumerate(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)whileTrue:#是否到了通讯录末尾
is_end = is_element_exist('com.tencent.mm:id/azb')
time.sleep(1)
remarks2=get_address_list(False)
remarks.extend(remarks2)ifis_end:breakremarks= set(remarks) #去重
print("通讯录昵称列表总数:", len(remarks))print("通讯录昵称列表:", remarks)
time.sleep(3)
dels=[]for inx, val inenumerate(remarks):
rt= ""
if inx ==0:
rt= is_delete(val, "1")else:
rt= is_delete(val, "")if rt isFalse: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) # 删除-删除我的人