python+uiautomation 获取微信好友昵称/备注

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') # 这里输入微信id/昵称/备注

# 判断通讯录是否有这个朋友
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  # 昵称
        # 备注,索引2是备注名,索引3是标签名
        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') # 这里输入微信id/昵称/备注 

# 模拟回车键来执行搜索操作
auto.SendKey(auto.Keys.VK_RETURN)

# 等待搜索结果出现,假设搜索结果显示在列表中
auto.WaitForExist(wechat_window, 5)  # 等待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)
  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值