1、通过 微信id/昵称/备注在 通讯录管理查找好友是否存在
也可以理解成通过微信id获取好友昵称/备注
import uiautomation as auto
wechat_window = auto.WindowControl(searchDepth=1, ClassName='WeChatMainWndForPC', SubName='微信')
wechat_window.ButtonControl(Name="通讯录").Click()
wechat_window.ListControl(Name="联系人").ButtonControl(Name="通讯录管理").Click()
contacts_window = auto.GetForegroundControl()
search_box = contacts_window.EditControl(Name="搜索")
search_box.Click()
search_box.SendKeys('mush')
text_control = contacts_window.TextControl(Name="无符合条件的结果")
if text_control.Exists():
print('查无此人')
close_button = contacts_window.ButtonControl(Name="关闭")
if close_button.Exists():
close_button.Click()
else:
children = contacts_window.ListControl().GetChildren()
if children:
first_name = children[0]
nick_name = first_name.TextControl().Name
remark_name = first_name.ButtonControl(foundIndex=2).Name
if remark_name:
print('remark_name---------', remark_name)
else:
print('nick_name---------', nick_name)
2、通过 在通讯录搜索框中搜索 微信id/昵称/备注 查找好友是否存在
import uiautomation as auto
wechat_window = auto.WindowControl(searchDepth=1, ClassName='WeChatMainWndForPC', SubName='微信')
wechat_window.ButtonControl(Name="通讯录").Click()
search_box = wechat_window.EditControl(Name="搜索")
search_box.Click()
search_box.SendKeys('mush')
auto.SendKey(auto.Keys.VK_RETURN)
auto.WaitForExist(wechat_window, 5)
search_results_list = auto.ListControl(searchFromControl=wechat_window)
if search_results_list.Exists():
auto.WaitForExist(wechat_window, 5)
items = search_results_list.GetChildren()
if items:
first_contact = items[0]
contacts_window = auto.GetForegroundControl()
if contacts_window.Name == '微信':
print('==============', first_contact.Name)
else:
print("-----", contacts_window.Name)
else:
print("未找到搜索结果.")
else:
print("未找到搜索结果列表控件.")
3、通过 在通讯录搜索框中搜索 微信id/昵称/备注 查找好友是否存在
3 和 2 的代码块作用是一样的,都可以理解成 *在通讯录搜索框中搜索 微信id/昵称/备注 查找好友是否存在* 或者是 *通过微信id获取好友昵称/备注*。
区别在于 3 将代码写成了方法,可以循环搜索好友昵称,以及增加了 ***wechat_window.SetActive()*** 可以使微信窗口在屏幕上处于最上层并获得焦点。
import time
import uiautomation as auto
def findNickname(wechat_id):
wechat_window = auto.WindowControl(searchDepth=1, ClassName='WeChatMainWndForPC', SubName='微信')
if not wechat_window.Exists(5):
print("微信窗口未找到或超时。")
return
wechat_window.SetActive()
time.sleep(1)
wechat_window.ButtonControl(Name="通讯录").Click()
search_box = wechat_window.EditControl(Name="搜索")
if search_box.Exists():
search_box.Click()
search_box.SendKeys(wechat_id)
auto.SendKeys('{ENTER}')
time.sleep(1)
else:
print("搜索框未找到或超时。")
return
search_results = auto.ListControl(searchFromControl=wechat_window)
if search_results.Exists():
items = search_results.GetChildren()
if items:
nickname_control = items[0]
contacts_window = auto.GetForegroundControl()
if contacts_window.Name == '微信':
print('昵称:', nickname_control.Name)
else:
print("昵称:", contacts_window.Name)
else:
print("搜索结果列表未找到或超时。")
if __name__ == "__main__":
_list = ['123', 'mush', 'aa']
for _uu in _list:
findNickname(_uu)