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)
Python结合UI Automation库(如Windows UI Automation或AutoIt)可以用于自动化Windows应用程序操作,例如在Windows上模拟鼠标点击、键盘输入等,以实现微信好友添加的功能。以下是基本步骤: 1. 安装必要的库:首先需要安装`pywinauto`库,它是一个用于控制Windows GUI应用的工具,可以方便地访问和操作Windows元素。 ```bash pip install pywinauto ``` 2. 导入并初始化:创建一个新的Python脚本,导入所需的模块,并设置目标窗口(微信聊天窗口或联系人列表)。 ```python from pywinauto.application import Application app = Application().start('weixin.exe') # 根据实际路径替换为微信的启动程序 ``` 3. 查找元素:定位到添加好友相关的按钮或菜单项。这通常涉及到查找特定的控件名称、文本、属性值或坐标位置。 ```python friends_button = app['FriendPanel'].ControlById('AddFriendButton') # 这里假设有个Add Friend的按钮ID ``` 4. 执行动作:模拟用户交互,比如点击添加好友按钮。 ```python friends_button.click() # 点击添加按钮 ``` 5. 输入好友信息:如果添加好友需要输入用户名或验证,可以找到相应的输入框进行填充。 ```python friend_input = app['SearchFriendEdit'] # 假设搜索栏的编辑框名是SearchFriendEdit friend_input.type_keys('朋友昵称') # 输入好友名字 ``` 6. 提交表单:确认添加,可能会有确认弹窗,此时需要处理这类事件。 请注意,实际操作可能因微信版本的不同而有所变化,某些功能可能被隐藏或者加密保护。此外,频繁的自动化操作可能违反微信的服务条款,因此在使用前务必确保了解相关规定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值