pywinauto操作微信发送文字,文件

  1. 发送文字
def sendText(id):
    app = Application(backend = 'uia').connect(title_re = '微信')
    dlg = app['微信']
    searchBox = dlg.child_window(title="搜索", control_type="Edit")
    searchBox.click_input()
    who = '文件传输助手'
    searchBox.type_keys(who, with_spaces = True)
    pyautogui.hotkey('enter')
    
    pyautogui.hotkey('ctrl', 'a')
    pyautogui.hotkey('backspace')
    # dlg.print_control_identifiers()
    sendMsg = 'How are you?'
    inputMsg = dlg.child_window(title = "文件传输助手", control_type = "Edit").wrapper_object()
    inputMsg.click_input()
    inputMsg.type_keys(sendMsg, with_spaces = True)
    sendBtn = dlg.child_window(title="发送(S)", control_type="Button")
    sendBtn.click_input()
    # ele = dlg.child_window(best_match='设置及其他')
    # ele.click_input()
  1. 发送文件,发送文件时选择联系人步骤和发送文字是相同的,通过搜索查找到第一个符合条件的联系人
def sendFile():
    app = Application(backend = 'uia').connect(title_re = '微信')
    win = app['微信']
    searchBox = win.child_window(title = "搜索", control_type = "Edit")
    searchBox.click_input()
    who = '文件传输助手'
    searchBox.type_keys(who, with_spaces = True)
    pyautogui.hotkey('enter')
    
    pyautogui.hotkey('ctrl', 'a')
    pyautogui.hotkey('backspace')
    
    sendFileBtn = win.child_window(title="发送文件", control_type="Button")
    sendFileBtn.click_input()
    switchOpen()
    clickSendButton()

发送文件时会弹出选择文件对话框,这时有两种方式,使用win32 或 pywinauto,以下是两种方式的代码

# 使用pywinauto选择文件
def switchOpen2():
    app = Application(backend = 'win32').connect(title_re = '打开')
    win = app['打开']
    input = win.child_window(class_name="Edit")
    filePath = r'd:\booking.csv'
    input.click_input()
    input.type_keys(filePath, with_spaces = True)
    win.child_window(title = "打开(&O)", class_name = "Button").click_input()
# 使用win32选择文件
def switchOpen():
    time.sleep(1)
    Dialog = win32gui.FindWindow('#32770', '打开')
    time.sleep(1)
    ComboBoxEx32 = win32gui.FindWindowEx(Dialog, 0, 'ComboBoxEx32', None)
    time.sleep(1)
    ComboBox = win32gui.FindWindowEx(ComboBoxEx32, 0, 'ComboBox', None)
    time.sleep(1)
    Edit = win32gui.FindWindowEx(ComboBox, 0, 'Edit', None)
    time.sleep(1)
    Button = win32gui.FindWindowEx(Dialog, 0, 'Button', None)  
    time.sleep(1)
    filePath = r'd:\booking.csv'
    win32gui.SendMessage(Edit, win32con.WM_SETTEXT, None, filePath)
    time.sleep(1)
    win32gui.SendMessage(Dialog, win32con.WM_COMMAND, 1, Button)

此时,操作的是这个对话框

  1. 选择文件完成后,弹出发送文件确认提示,此时点击 发送 按钮
    pywinauto操作微信发送文字,文件_第1张图片
def clickSendButton():
    app = Application(backend = 'uia').connect(title_re = '微信')
    win = app['微信']
    win.child_window(title="发送(1)", control_type="Button").click_input()

你可能感兴趣的:(微信,python)