pywinauto
pywinauto
pywinauto是一组用于自动化Microsoft Windows GUI的python模块。 最简单的是,它允许您将鼠标和键盘操作发送到窗口对话框和控件,来完成我们自动化操作。
官方文档
https://www.kancloud.cn/gnefnuy/pywinauto_doc/1193036
程序对象使用工具
窗口操作
from pywinauto.application import Application app = Application("uia").start(r"D:\++\Navicat Premium 12\navicat.exe") # 打开navicat dlg = app["TNavicat Main Form"] # 窗口最大话 # dlg.maximize() # 窗口最小话 # dlg.minimize() # 恢复正常 # dlg.restore() #查找窗口显示状态:最大化是:1 正常是:0 # a = dlg.get_show_state() # print(a) # 获取当前窗口显示的坐标 # rect = dlg.rectangle() # print(rect) # 关闭窗口 # dlg.close()
窗口上的控件选择
from pywinauto.application import Application app = Application("uia").start(r"D:\++\Navicat Premium 12\navicat.exe") # 打开navicat dlg = app["TNavicat Main Form"] # 打印控件 # dlg.print_control_identifiers() # 选择控件 #01 # menu = dlg["Menu"] # menu.print_control_identifiers() #02 # menu = dlg["Action Main Menu Bar"] # print(menu.print_control_identifiers()) #>>>>>>报错 # file = dlg["Menu"]["MenuItem0"] # file.print_control_identifiers() #03 # file = menu.child_window(title="文件", control_type="MenuItem") # file.print_control_identifiers()
截图
from pywinauto.application import Application app = Application("uia").start(r"D:\++\Navicat Premium 12\navicat.exe") # 打开navicat dlg = app["TNavicat Main Form"] # 选择菜单 menu = dlg["Menu"] # 选择菜单项:文件 file = menu.child_window(title="文件", control_type="MenuItem") # 截图 pic = dlg.capture_as_image() print(pic) pic.save("tu.png")
控件操作
# from pywinauto.application import Application import pywinauto app = pywinauto.Application().start(r"notepad.exe") # 选择主窗口 dlg = app["Notepad"] # dlg.print_control_identifiers() # dlg["Edit"].type_keys("python,6666") # 替换操作 # dlg.menu_select("编辑->替换(R)") # 选择替换窗口 # app["替换"].print_control_identifiers() # 选择查找编辑框 # app["替换"]["Edit1"].type_keys('6666') # 选择替换编辑框 # app["替换"]["Edit2"].type_keys('99999') # 选择全部替换按钮,进行点击 # app["替换"]["Button3"].click()
模拟键盘基本操作
from pywinauto.keyboard import send_keys import time # 按F1 # send_keys('{F1}') # send_keys('{VK_F1}') # 通过按键 打开cmd 进入python send_keys("{VK_LWIN}") send_keys("cmd") send_keys("{VK_RETURN}") # 回车 # send_keys("{VK_LWIN}cmd{VK_RETURN}") # 简写 time.sleep(2) send_keys("python") send_keys("{VK_RETURN}") # 回车
键盘修饰符
from pywinauto.keyboard import send_keys """ ^ ---->>ctrl + ---->>shift % ---->>alt """ # send_keys("^a") send_keys("^+n")
模拟鼠标操作
from pywinauto import mouse # 鼠标单击 mouse.click(coords=(600,23)) # (x,y)
文件上传
import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains # 暴力点击 import pywinauto from pywinauto.keyboard import send_keys driver = webdriver.Chrome() driver.get('https://www.baidu.com') driver.maximize_window() b = driver.find_element_by_xpath('//*[@id="form"]/span[1]/span[1]') time.sleep(1) a = driver.find_element(By.XPATH,'//*[@id="form"]/div/div[2]/div[2]/input') # ActionChains(driver).click(a).perform() # 暴力点击 # app = pywinauto.Desktop() # dlg = app["打开"] # dlg.print_control_identifiers() # # #选择文件地址输入框 # dlg["Toolbar3"].click() # send_keys(r"C:\Users\hui\Pictures") # send_keys("{VK_RETURN}") # # # 选中文件名输入框 # # dlg["Edit"].type_keys('Photo.jpg') # # # 点击打开按钮 # dlg["打开(&O)"].click()
多文件上传
import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains # 暴力点击 import pywinauto from pywinauto.keyboard import send_keys driver = webdriver.Chrome() driver.get('https://www.baidu.com') driver.maximize_window() driver.find_element_by_xpath('//*[@id="form"]/span[1]/span[1]').click() time.sleep(1) a = driver.find_element(By.XPATH,'//*[@id="form"]/div/div[2]/div[2]/input') ActionChains(driver).click(a).perform() # 暴力点击 def upload_files(file_path,file,*args): app = pywinauto.Desktop() dlg = app["打开"] # dlg.print_control_identifiers() #选择文件地址输入框 dlg["Toolbar3"].click() send_keys(file_path) send_keys("{VK_RETURN}") # 选中文件名输入框 dlg["Edit"].type_keys('"{}"'.format(file)) for i in args: send_keys('"{}"'.format(i)) # 点击打开按钮 # dlg["打开(&O)"].click() upload_files(r"C:\Users\hui\Pictures",'Photo.jpg','SnxFVyWwbLq.jpg')
幻想毫无价值,计划渺如尘埃,目标不可能达到。这一切的一切毫无意义——除非我们付诸行动。