前言
桌面应用的操作都是基于控件操作,先找到对应的窗口,基于操控查找框架即可操作了。
查看窗口控件
先学会如何查看窗口框架,可以用inspect.exe 查看窗口的层级结构
以打开的记事本为例
也可以通过print_ctrl_ids()
方法 (另外一个print_control_identifiers()
功能一样)查看当前窗口下的控件
from pywinauto import Application
import time
app = Application('uia').start("notepad.exe")
win = app.window(title_re="无标题 - 记事本")
# 获取当前窗口下控件
print(win.print_ctrl_ids())
结果如下
Dialog - '无标题 - 记事本' (L397, T290, R1906, B1054)
['Dialog', '无标题 - 记事本', '无标题 - 记事本Dialog']
child_window(title="无标题 - 记事本", control_type="Window")
|
| Edit - '文本编辑器' (L405, T341, R1898, B1046)
| ['Edit']
| child_window(title="文本编辑器", auto_id="15", control_type="Edit")
| |
| | ScrollBar - '垂直滚动条' (L1881, T341, R1898, B1029)
| | ['ScrollBar', '垂直滚动条', '垂直滚动条ScrollBar', 'ScrollBar0', 'ScrollBar1']
| | child_window(title="垂直滚动条", auto_id="NonClientVerticalScrollBar", control_type="ScrollBar")
| | |
| | | Button - '上一行' (L1881, T341, R1898, B358)
| | | ['Button', '上一行Button', '上一行', 'Button0', 'Button1']
| | | child_window(title="上一行", auto_id="UpButton", control_type="Button")
| | |
| | | Button - '下一行' (L1881, T1012, R1898, B1029)
| | | ['Button2', '下一行', '下一行Button']
| | | child_window(title="下一行", auto_id="DownButton", control_type="Button")
| |
| | ScrollBar - '水平滚动条' (L405, T1029, R1881, B1046)
| | ['ScrollBar2', '水平滚动条', '水平滚动条ScrollBar']
| | child_window(title="水平滚动条", auto_id="NonClientHorizontalScrollBar", control_type="ScrollBar")
| | |
| | | Button - '左移一列' (L405, T1029, R422, B1046)
| | | ['Button3', '左移一列Button', '左移一列']
| | | child_window(title="左移一列", auto_id="UpButton", control_type="Button")
| | |
| | | Button - '右移一列' (L1864, T1029, R1881, B1046)
| | | ['Button4', '右移一列Button', '右移一列']
| | | child_window(title="右移一列", auto_id="DownButton", control_type="Button")
| |
| | Thumb - '调整框大小' (L1881, T1029, R1898, B1046)
| | ['Thumb', '调整框大小Thumb', '调整框大小']
| | child_window(title="调整框大小", control_type="Thumb")
|
| TitleBar - '' (L421, T293, R1898, B321)
| ['TitleBar']
| |
| | Menu - '系统' (L405, T298, R427, B320)
| | ['系统', '系统Menu', 'Menu', '系统0', '系统1', 'Menu0', 'Menu1']
| | child_window(title="系统", auto_id="MenuBar", control_type="MenuBar")
| | |
| | | MenuItem - '系统' (L405, T298, R427, B320)
| | | ['系统2', '系统MenuItem', 'MenuItem', 'MenuItem0', 'MenuItem1']
| | | child_window(title="系统", control_type="MenuItem")
| |
| | Button - '最小化' (L1759, T291, R1806, B321)
| | ['Button5', '最小化Button', '最小化']
| | child_window(title="最小化", control_type="Button")
| |
| | Button - '最大化' (L1806, T291, R1852, B321)
| | ['Button6', '最大化Button', '最大化']
| | child_window(title="最大化", control_type="Button")
| |
| | Button - '关闭' (L1852, T291, R1899, B321)
| | ['Button7', '关闭', '关闭Button']
| | child_window(title="关闭", control_type="Button")
|
| Menu - '应用程序' (L405, T321, R1898, B340)
| ['应用程序', 'Menu2', '应用程序Menu']
| child_window(title="应用程序", auto_id="MenuBar", control_type="MenuBar")
| |
| | MenuItem - '文件(F)' (L405, T321, R457, B340)
| | ['文件(F)', 'MenuItem2', '文件(F)MenuItem']
| | child_window(title="文件(F)", control_type="MenuItem")
| |
| | MenuItem - '编辑(E)' (L457, T321, R510, B340)
| | ['编辑(E)MenuItem', '编辑(E)', 'MenuItem3']
| | child_window(title="编辑(E)", control_type="MenuItem")
| |
| | MenuItem - '格式(O)' (L510, T321, R566, B340)
| | ['格式(O)', '格式(O)MenuItem', 'MenuItem4']
| | child_window(title="格式(O)", control_type="MenuItem")
| |
| | MenuItem - '查看(V)' (L566, T321, R620, B340)
| | ['查看(V)MenuItem', 'MenuItem5', '查看(V)']
| | child_window(title="查看(V)", control_type="MenuItem")
| |
| | MenuItem - '帮助(H)' (L620, T321, R675, B340)
| | ['帮助(H)', 'MenuItem6', '帮助(H)MenuItem']
| | child_window(title="帮助(H)", control_type="MenuItem")
None
编辑框输入内容
我们想在编辑器输入内容,可以先查看编辑区的属性
对应我们上面看到win.print_ctrl_ids() 输出的内容
| Edit - '文本编辑器' (L405, T341, R1898, B1046)
| ['Edit']
| child_window(title="文本编辑器", auto_id="15", control_type="Edit")
set_text()在编辑器输入:hello world
from pywinauto import Application
app = Application('uia').start("notepad.exe")
win = app.window(title_re="无标题 - 记事本")
# 获取当前窗口下控件
# print(win.print_ctrl_ids())
# 输入内容
win.child_window(title="文本编辑器").set_text("hello world")
也可以用中括号的方式操作控件
win["Edit"].set_text("hello world")
标签:control,控件,title,windows,pywinauto,Button,window,child,type
From: https://www.cnblogs.com/yoyoketang/p/17648190.html