app.kill()
二、backend选择 和 控件查看工具inspect
1.backend选择和inspect介绍
我们安装好Pywinauto之后,首先要确定哪种可访问性技术(backend)可以用于我们的应用程序,在windows上受支持的有两种:
Win32 API (backend= "win32"
) 默认的backend
MS UI Automation (backend="uia"
)
如果不能确定程序到底适用于那种backend,可以借助于GUI对象检查工具来做,常用的检查工具有Inspect.ex,Spy++ ,下载地址:https://github.com/blackrosezy/gui-inspect-tool
将inspect左上角的下拉列表中切换到“UI Automation”,然后鼠标点一下你需要测试的程序窗体,inspect就会显示相关信息,如下图所示。说明backend为uia程序里面的任意一个部位其实都是控件,在inspect的控件树中都可以找到,是一层一层分级别的,可以一个个点开所有控件
2.打印元素
我们拿到控件后,是可以将该控件下的所有子控件及其属性以树形结构打印出来的:
win_main_Dialog = app.window(class_name='WeChatMainWndForPC')
print(win_main_Dialog.is_dialog)
win_main_Dialog.draw_outline(colour = 'red')
win_main_Dialog. print_control_identifiers(depth=None, filename=None)
depth:打印的深度,缺省时打印最大深度。
filename:将返回的标识存成文件(生成的文件与当前运行的脚本在同一个路径下)
eg:dlg. print_control_identifiers(filename =’a.txt’)
打印出来的文档树就是inspect中的控件树完全展开的样子,都是有层级的,和微信程序中的各个元素是一一对应的
三、控件查找定位
win_main_Dialog = app.window(class_name='WeChatMainWndForPC')
chat_list = win_main_Dialog.child_window(control_type='List', title='会话')
first = chat_list.items()[0]
chat_list.scroll(direction='down', amount='page')
details_page = win_main_Dialog.child_window(class_name='ContactProfileWnd')
we_id = details_page.child_window(title="微信号:", control_type="Text").parent().children()[1].window_text()
alia = details_page.child_window(title="微信号:", control_type="Text").parent().parent().children()[0].children()[0].window_text()
edit_btn = details_page.child_window(title="备 注", control_type="Text").parent().children()[1]
edit_btn.click_input()
btn_modify_name_edit = edit_btn
btn_modify_name_edit.type_keys('^a').type_keys('备注名字', with_spaces=True)
btn_return = win_main_Dialog.child_window(control_type='ToolBar').parent().descendants(control_type='Button')
btn_return[0].click_input()
title="微信团队"
class_name='WeChatLoginWndForPC'
control_type="Window"
control_type="Text"
control_type="Button"
control_type="List"
control_type="ListItem"
control_type='MenuItem'
control_type='ToolBar'
auto_id='FileTypeControlHost'
控件常见特征
四、控件自带的的方法
1. 点击和输入
# 左点击,可以点进源码,还有double_click_input,right_click_input等
edit_btn.click_input()
# 先ctrl+a选中所有然后再type_keys替换,和我们选中然后修改一样的
edit_btn.type_keys('^a').type_keys('备注名字', with_spaces=True)
SHIFT +
CTRL ^
ALT %
空格键 {SPACE}
BACKSPACE {BACKSPACE}、{BS} or {BKSP}
BREAK {BREAK}
CAPS LOCK {CAPSLOCK}
DEL or DELETE {DELETE} or {DEL}
DOWN ARROW {DOWN}
END {END}
ENTER {ENTER} or ~
ESC {ESC}
HELP {HELP}
HOME {HOME}
INS or INSERT {INSERT} or {INS}
LEFT ARROW {LEFT}
NUM LOCK {NUMLOCK}
PAGE DOWN {PGDN}
PAGE UP {PGUP}
PRINT SCREEN {PRTSC}
RIGHT ARROW {RIGHT}
SCROLL LOCK {SCROLLLOCK}
TAB {TAB}
UP ARROW {UP}
+ {ADD}
- {SUBTRACT}
* {MULTIPLY}
/ {DIVIDE}
常用快捷键
2.对控件截图并保存
ctrl_qrcode = self.win_login.child_window(title='二维码', control_type='Image')
if ctrl_qrcode.exists():
ctrl_qrcode.capture_as_image().save(img_path)
3.窗口的等待
窗口加载需要时间,我们又不能一直sleep就需要等待,等待窗口出现或者等待窗口关闭:
save_dialog.wait('ready',timeout=2)
save_dialog.close()
save_dialog.wait_not('visible')
# 'exists':窗口是有效的句柄
# 'visible':窗口未隐藏,常用
# 'enabled':未禁用窗口
# 'ready':窗口可见并启用,常用
# 'active':窗口处于活动状态
dlg = app.top_window()
print(dlg.class_name())
chat_list.scroll(direction='up', amount='page')
五、鼠标操作
from pywinauto import mouse
常见操作:
mouse.move(coords=(x, y))
mouse.click(button='left', coords=(40, 40))
mouse.double_click(button='left', coords=(140, 40))
mouse.press(button='left', coords=(140, 40))
mouse.release(button='left', coords=(300, 40))
mouse.right_click(coords=(400, 400))
mouse.wheel_click(coords=(400, 400))
mouse.scroll(coords=(1200,300),wheel_dist=-3)
def mouse_scroll(control, distance):
rect = control.rectangle()
cx = int((rect.left+rect.right)/2)
cy = int((rect.top + rect.bottom)/2)
mouse.scroll(coords=(cx, cy), wheel_dist=distance)
mouse_scroll(control=win_main_Dialog.child_window(control_type='List', title='联系人'), distance=-5)
六、键盘操作
和控件自己的type_keys方法效果一样,但是更快,那个是从前到后啪啪啪的输入,这个是一下就出来了那种
在发送文件和图片的时候可以使用键盘模块,复制粘贴,比啪啪啪输入路径再发送速度快多了
import keyboard
import io
for line in io.StringIO(msg):
keyboard.write(line.strip()) #
keyboard.send('ctrl+enter')
keyboard.write(chat_name)
keyboard.send('enter')
keyboard.send('ctrl+v')
原文地址:https://www.cnblogs.com/xp1315458571/p/13892205.html