官方文档: https://pywinauto.readthedocs.io/en/latest/contents.html
[安装]
pip install pywinauto
[使用]
1.连接
使用实例:
1)自动打开微信链接
import random import time from pywinauto import mouse, keyboard import pywinauto.findwindows from pywinauto.application import Application import pyautogui # 连接方式1) # app = Application(backend='uia').connect(process=19004) # 连接方式2) app = Application(backend='uia').connect(path=r'D:\software\WeChat\WeChat.exe') # 该窗口中的对话框 # print(app.windows()) # Chrome_WidgetWin_0 # # 打开 # app = Application(backend='uia').start('D:\software\WeChat\WeChat.exe') # time.sleep(5) # 拿到微信主窗口 win_main_dialog = app.window(title='微信', class_name='WeChatMainWndForPC') # 判断是否未dialog, 一个微信就是一个dialog -->窗口 # print(win_main_dialog.is_dialog) # print(win_main_dialog.wrapper_object()) # 给空间画一个红色框便于看出 win_main_dialog.draw_outline(colour='red') # 打印当前所有空间和属性 # win_main_dialog.print_control_identifiers(depth=None, filename=None) time.sleep(1) ''' 一般使用click_input代替click''' def get_element_position(ele): """获取元素的中心点位置""" element_position = ele.rectangle() # 算出中心点位置 center_position = (int((element_position.left + element_position.right) / 2), int((element_position.top + element_position.bottom) / 2)) return center_position def send_msg(): # 找到文件传输助手 file_helper_ele = win_main_dialog.child_window(title="文件传输助手已置顶", control_type="ListItem") mouse.click(button='left', coords=get_element_position(file_helper_ele)) # 获取输入框元素, 并模拟输入 edit_ele = win_main_dialog.child_window(title="文件传输助手", control_type="Edit") time.sleep(2) edit_ele.type_keys("hello") keyboard.send_keys("{ENTER}") def click_buttons(): '''分别进行点击''' win_main_dialog.child_window(title="聊天", control_type="Button").click_input() time.sleep(2) win_main_dialog.child_window(title="通讯录", control_type="Button").click_input() time.sleep(2) win_main_dialog.child_window(title="朋友圈", control_type="Button").click_input() time.sleep(2) def gui(): # 使用pyautogui方式是比较固定(坐标)的方式 pyautogui.click(x=622, y=183, clicks=1, interval=0.1) time.sleep(random.randint(3, 5)) pyautogui.click(x=756, y=78, clicks=1, interval=0.1) ''' depth: 打印深度, 缺省最大深度 filenmae: 返回的标识存成文件, 可以指定 eg:a.txt ''' # 查找主窗口下的某个窗口 # chat_list = win_main_dialog.child_window(control_type='List', title='会话') # first = chat_list.items()[0] # 第一个聊天项, 支持循环, 索引 # chat_list.scroll(direction='down', amount='page') # 向下滚动一页 # first.focus() def main(): # 找到聊天框 chat_list_ele = win_main_dialog.child_window(title="聊天", control_type="Button") mouse.click(button='left', coords=get_element_position(chat_list_ele)) time.sleep(1) # 找到指定聊天人 ln = win_main_dialog.child_window(title="Learning已置顶", control_type="ListItem") mouse.click(button='left', coords=get_element_position(ln)) time.sleep(1) # 找到指定链接并点击 lk = win_main_dialog.child_window(title=r"https://www.cnblogs.com/xingxia", control_type="ListItem") mouse.click(button='left', coords=get_element_position(lk)) time.sleep(random.randint(3, 5)) # 关闭内置浏览器 # 1) 查询当前内置浏览器窗格信息 wins = pywinauto.findwindows.find_elements(backend='uia') print('所有窗口:', wins) wins = pywinauto.findwindows.find_elements(backend='uia') # print('所有窗口:', wins) pid = 0 for win in wins: print('=======================') print(dir(win)) print(win.name) # 窗口名称 # print(win.control_id) # 都是0 # print(win.process_id) # print(win.automation_id) print(win.class_name) # 类名 # print(win.control_id) # 都是0 print(win.runtime_id) # (id, 进程ID) if win.class_name == 'Chrome_WidgetWin_0': pid = win.runtime_id[1] app2 = Application(backend='uia').connect(handle=pid) # print(app2.window().print_control_identifiers()) app2.window().child_window(title='关闭', control_type='Button', found_index=0).click_input() if __name__ == '__main__': main() # 自动化主流程 # send_msg() # 发送消息 # click_buttons() # gui()
说明:
标签:control,pywinauto,自动化,dialog,win,print,main,click From: https://www.cnblogs.com/xingxia/p/18330263/python_pywinauto