🔥PC端自动化测试实战教程-11-pywinauto操作弹出新窗口上按钮(详细教程)

230 阅读17分钟

1.简介

在实际工作中,我们不仅仅是在启动软件的界面窗口进行操作,而且还需要在新的界面窗口中进行操作,那么如何进行操作呢?这就是宏哥今天这一篇文章主要讲解和分享的知识点。怎么会出现新的界面或者窗口呢,这是因为我们在点击某些按钮后,就会弹出一个新的界面窗口。

2.如何操作新的界面窗口?

如何操作,其实很简单的,操作软件原窗口,我们就切换到原窗口,操作新窗口,同样的道理,我们只需要切换到新的窗口就可以进行操作了。

3.项目实战

因为宏哥的系统是Windows11,所以这里为了减少不必要的麻烦主要还是通过Windows安装的Notepad++编辑器进行演示和实践。

3.1测试场景

 大致测试场景:启动电脑安装的Notepad++编辑器,然后点击“设置(T)”,弹出级联菜单,点击“首选项...”,就会弹出首选项的一个新窗口,然后操作这个新窗口的“关闭”按钮,关闭窗口,最后再在编辑器输入“北京-宏哥”。

3.2查看窗口结构树

1.为了小伙伴或者童鞋们看的清楚,以及方便宏哥演示,宏哥这里还是采用前边两种方法的代码打印,如下图所示:

2.打印内容,如下:

D:\Python\python.exe D:/Demo/test.py
Control Identifiers:

Dialog - '新文件 1 - Notepad++'    (L-9, T-9, R1929, B1029)
['新文件 1 - Notepad++', '新文件 1 - Notepad++Dialog', 'Dialog', 'Dialog0', 'Dialog1']
child_window(title="新文件 1 - Notepad++", control_type="Window")
   | 
   | Dialog - '首选项'    (L467, T285, R1453, B790)
   | ['首选项Dialog', 'Dialog2', '首选项']
   | child_window(title="首选项", control_type="Window")
   |    | 
   |    | ListBox - ''    (L488, T337, R628, B707)
   |    | ['ListBox']
   |    | child_window(auto_id="6002", control_type="List")
   |    |    | 
   |    |    | ListItem - '常用'    (L490, T339, R626, B355)
   |    |    | ['常用ListItem', 'ListItem', '常用', 'ListItem0', 'ListItem1']
   |    |    | child_window(title="常用", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '编辑'    (L490, T355, R626, B371)
   |    |    | ['编辑', 'ListItem2', '编辑ListItem']
   |    |    | child_window(title="编辑", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '暗黑模式'    (L490, T371, R626, B387)
   |    |    | ['ListItem3', '暗黑模式', '暗黑模式ListItem']
   |    |    | child_window(title="暗黑模式", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '边距/边框/边缘'    (L490, T387, R626, B403)
   |    |    | ['边距/边框/边缘ListItem', 'ListItem4', '边距/边框/边缘']
   |    |    | child_window(title="边距/边框/边缘", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '新建'    (L490, T403, R626, B419)
   |    |    | ['新建', 'ListItem5', '新建ListItem']
   |    |    | child_window(title="新建", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '默认目录'    (L490, T419, R626, B435)
   |    |    | ['ListItem6', '默认目录ListItem', '默认目录']
   |    |    | child_window(title="默认目录", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '最近文件记录'    (L490, T435, R626, B451)
   |    |    | ['最近文件记录', '最近文件记录ListItem', 'ListItem7']
   |    |    | child_window(title="最近文件记录", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '文件关联'    (L490, T451, R626, B467)
   |    |    | ['ListItem8', '文件关联', '文件关联ListItem']
   |    |    | child_window(title="文件关联", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '语言'    (L490, T467, R626, B483)
   |    |    | ['ListItem9', '语言ListItem', '语言']
   |    |    | child_window(title="语言", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '高亮'    (L490, T483, R626, B499)
   |    |    | ['高亮ListItem', 'ListItem10', '高亮']
   |    |    | child_window(title="高亮", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '打印'    (L490, T499, R626, B515)
   |    |    | ['ListItem11', '打印ListItem', '打印']
   |    |    | child_window(title="打印", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '搜索'    (L490, T515, R626, B531)
   |    |    | ['搜索', 'ListItem12', '搜索ListItem']
   |    |    | child_window(title="搜索", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '备份'    (L490, T531, R626, B547)
   |    |    | ['备份ListItem', 'ListItem13', '备份']
   |    |    | child_window(title="备份", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '自动完成'    (L490, T547, R626, B563)
   |    |    | ['自动完成', 'ListItem14', '自动完成ListItem']
   |    |    | child_window(title="自动完成", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '多实例 & 日期'    (L490, T563, R626, B579)
   |    |    | ['多实例 & 日期ListItem', '多实例 & 日期', 'ListItem15']
   |    |    | child_window(title="多实例 & 日期", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '定界符'    (L490, T579, R626, B595)
   |    |    | ['ListItem16', '定界符ListItem', '定界符']
   |    |    | child_window(title="定界符", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '云同步与链接'    (L490, T595, R626, B611)
   |    |    | ['ListItem17', '云同步与链接', '云同步与链接ListItem']
   |    |    | child_window(title="云同步与链接", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '搜索引擎'    (L490, T611, R626, B627)
   |    |    | ['ListItem18', '搜索引擎', '搜索引擎ListItem']
   |    |    | child_window(title="搜索引擎", control_type="ListItem")
   |    |    | 
   |    |    | ListItem - '其他'    (L490, T627, R626, B643)
   |    |    | ['ListItem19', '其他ListItem', '其他']
   |    |    | child_window(title="其他", control_type="ListItem")
   |    | 
   |    | Button - '关闭'    (L916, T747, R995, B775)
   |    | ['关闭', '关闭Button', 'Button', 'Button0', 'Button1', '关闭0', '关闭1', '关闭Button0', '关闭Button1']
   |    | child_window(title="关闭", auto_id="6001", control_type="Button")
   |    | 
   |    | Static - '界面语言'    (L662, T344, R799, B360)
   |    | ['Static', '界面语言', '界面语言Static', '界面语言0', '界面语言1', 'Static0', 'Static1', '界面语言Static0', '界面语言Static1']
   |    | child_window(title="界面语言", auto_id="6123", control_type="Text")
   |    | 
   |    | ComboBox - '界面语言'    (L803, T340, R1011, B364)
   |    | ['ComboBox', '界面语言ComboBox', '界面语言2']
   |    | child_window(title="界面语言", auto_id="6124", control_type="ComboBox")
   |    |    | 
   |    |    | Static - '界面语言'    (L3, T3, R184, B21)
   |    |    | ['Static2', '界面语言3', '界面语言Static2']
   |    |    | child_window(title="界面语言", control_type="Text")
   |    |    | 
   |    |    | Button - '打开'    (L991, T341, R1010, B363)
   |    |    | ['打开', 'Button2', '打开Button']
   |    |    | child_window(title="打开", auto_id="DropDown", control_type="Button")
   |    | 
   |    | GroupBox - '工具栏'    (L758, T374, R1015, B568)
   |    | ['工具栏GroupBox', '工具栏', 'GroupBox', 'GroupBox0', 'GroupBox1']
   |    | child_window(title="工具栏", auto_id="6101", control_type="Group")
   |    | 
   |    | CheckBox - '隐藏'    (L770, T390, R935, B410)
   |    | ['隐藏', '隐藏CheckBox', 'CheckBox', 'CheckBox0', 'CheckBox1']
   |    | child_window(title="隐藏", auto_id="6102", control_type="CheckBox")
   |    | 
   |    | RadioButton - 'Fluent UI 图标:小'    (L770, T420, R970, B440)
   |    | ['Fluent UI 图标:小RadioButton', 'RadioButton', 'Fluent UI 图标:小', 'RadioButton0', 'RadioButton1']
   |    | child_window(title="Fluent UI 图标:小", auto_id="6103", control_type="RadioButton")
   |    | 
   |    | RadioButton - 'Fluent UI 图标:大'    (L770, T448, R961, B468)
   |    | ['RadioButton2', 'Fluent UI 图标:大', 'Fluent UI 图标:大RadioButton']
   |    | child_window(title="Fluent UI 图标:大", auto_id="6104", control_type="RadioButton")
   |    | 
   |    | RadioButton - 'Fluent UI 填充图标:小'    (L770, T476, R970, B496)
   |    | ['Fluent UI 填充图标:小', 'RadioButton3', 'Fluent UI 填充图标:小RadioButton']
   |    | child_window(title="Fluent UI 填充图标:小", auto_id="6129", control_type="RadioButton")
   |    | 
   |    | RadioButton - 'Fluent UI 填充图标:大'    (L770, T504, R961, B524)
   |    | ['RadioButton4', 'Fluent UI 填充图标:大RadioButton', 'Fluent UI 填充图标:大']
   |    | child_window(title="Fluent UI 填充图标:大", auto_id="6130", control_type="RadioButton")
   |    | 
   |    | RadioButton - '标准图标:小'    (L770, T532, R978, B552)
   |    | ['RadioButton5', '标准图标:小', '标准图标:小RadioButton']
   |    | child_window(title="标准图标:小", auto_id="6105", control_type="RadioButton")
   |    | 
   |    | GroupBox - '标签栏'    (L1048, T342, R1356, B654)
   |    | ['标签栏GroupBox', 'GroupBox2', '标签栏']
   |    | child_window(title="标签栏", auto_id="6106", control_type="Group")
   |    | 
   |    | CheckBox - '隐藏标签栏'    (L1059, T360, R1248, B380)
   |    | ['隐藏标签栏CheckBox', '隐藏标签栏', 'CheckBox2']
   |    | child_window(title="隐藏标签栏", auto_id="6118", control_type="CheckBox")
   |    | 
   |    | CheckBox - '多行显示'    (L1059, T388, R1294, B408)
   |    | ['多行显示', '多行显示CheckBox', 'CheckBox3']
   |    | child_window(title="多行显示", auto_id="6119", control_type="CheckBox")
   |    | 
   |    | CheckBox - '垂直显示'    (L1059, T412, R1294, B432)
   |    | ['垂直显示', '垂直显示CheckBox', 'CheckBox4']
   |    | child_window(title="垂直显示", auto_id="6120", control_type="CheckBox")
   |    | 
   |    | CheckBox - '缩小'    (L1059, T438, R1248, B458)
   |    | ['缩小', 'CheckBox5', '缩小CheckBox']
   |    | child_window(title="缩小", auto_id="6107", control_type="CheckBox")
   |    | 
   |    | CheckBox - '自定义图标'    (L1059, T464, R1346, B484)
   |    | ['自定义图标', '自定义图标CheckBox', 'CheckBox6']
   |    | child_window(title="自定义图标", auto_id="6128", control_type="CheckBox")
   |    | 
   |    | CheckBox - '锁定 (不允许拖放)'    (L1059, T490, R1294, B510)
   |    | ['锁定 (不允许拖放)CheckBox', 'CheckBox7', '锁定 (不允许拖放)']
   |    | child_window(title="锁定 (不允许拖放)", auto_id="6108", control_type="CheckBox")
   |    | 
   |    | CheckBox - '非当前标签变暗'    (L1059, T516, R1294, B536)
   |    | ['非当前标签变暗', '非当前标签变暗CheckBox', 'CheckBox8']
   |    | child_window(title="非当前标签变暗", auto_id="6109", control_type="CheckBox")
   |    | 
   |    | CheckBox - '为当前标签绘制颜色条'    (L1059, T542, R1299, B562)
   |    | ['为当前标签绘制颜色条', '为当前标签绘制颜色条CheckBox', 'CheckBox9']
   |    | child_window(title="为当前标签绘制颜色条", auto_id="6110", control_type="CheckBox")
   |    | 
   |    | CheckBox - '标签显示关闭按钮'    (L1059, T568, R1292, B588)
   |    | ['标签显示关闭按钮', 'CheckBox10', '标签显示关闭按钮CheckBox']
   |    | child_window(title="标签显示关闭按钮", auto_id="6112", control_type="CheckBox")
   |    | 
   |    | CheckBox - '双击关闭标签'    (L1059, T594, R1292, B614)
   |    | ['双击关闭标签', '双击关闭标签CheckBox', 'CheckBox11']
   |    | child_window(title="双击关闭标签", auto_id="6113", control_type="CheckBox")
   |    | 
   |    | CheckBox - '关闭最后一个标签时退出'    (L1059, T620, R1348, B640)
   |    | ['关闭最后一个标签时退出CheckBox', '关闭最后一个标签时退出', 'CheckBox12']
   |    | child_window(title="关闭最后一个标签时退出", auto_id="6121", control_type="CheckBox")
   |    | 
   |    | CheckBox - '显示状态栏'    (L770, T594, R998, B614)
   |    | ['显示状态栏', 'CheckBox13', '显示状态栏CheckBox']
   |    | child_window(title="显示状态栏", auto_id="6111", control_type="CheckBox")
   |    | 
   |    | CheckBox - '隐藏菜单栏 (按下 Alt 或 F10 显示)'    (L1048, T668, R1430, B688)
   |    | ['隐藏菜单栏 (按下 Alt 或 F10 显示)CheckBox', 'CheckBox14', '隐藏菜单栏 (按下 Alt 或 F10 显示)']
   |    | child_window(title="隐藏菜单栏 (按下 Alt 或 F10 显示)", auto_id="6122", control_type="CheckBox")
   |    | 
   |    | TitleBar - ''    (L470, T288, R1450, B317)
   |    | ['TitleBar', 'TitleBar0', 'TitleBar1']
   |    |    | 
   |    |    | Button - '关闭'    (L1407, T286, R1451, B317)
   |    |    | ['关闭2', '关闭Button2', 'Button3']
   |    |    | child_window(title="关闭", control_type="Button")
   | 
   | Pane - ''    (L2, T115, R1918, B989)
   | ['界面语言Pane', 'Pane', 'Pane0', 'Pane1']
   | 
   | TabControl - 'Tab'    (L0, T83, R1920, B991)
   | ['Tab', 'TabControl', 'TabTabControl']
   | child_window(title="Tab", control_type="Tab")
   |    | 
   |    | TabItem - '新文件 1'    (L2, T85, R112, B113)
   |    | ['TabItem', '新文件 1TabItem', '新文件 1']
   |    | child_window(title="新文件 1", control_type="TabItem")
   | 
   | StatusBar - ''    (L0, T991, R1920, B1020)
   | ['StatusBar', 'Normal text fileStatusBar']
   |    | 
   |    | Static - 'Normal text file'    (L0, T993, R974, B1020)
   |    | ['Normal text fileStatic', 'Normal text file', 'Static3']
   |    | child_window(title="Normal text file", control_type="Text")
   |    | 
   |    | Static - 'length : 0    lines : 1'    (L976, T993, R1249, B1020)
   |    | ['Static4', 'length : 0    lines : 1Static', 'length : 0    lines : 1']
   |    | child_window(title="length : 0    lines : 1", control_type="Text")
   |    | 
   |    | Static - 'Ln : 1    Col : 1    Pos : 1'    (L1251, T993, R1574, B1020)
   |    | ['Ln : 1    Col : 1    Pos : 1Static', 'Ln : 1    Col : 1    Pos : 1', 'Static5']
   |    | child_window(title="Ln : 1    Col : 1    Pos : 1", control_type="Text")
   |    | 
   |    | Static - 'Windows (CR LF)'    (L1576, T993, R1712, B1020)
   |    | ['Windows (CR LF)Static', 'Windows (CR LF)', 'Static6']
   |    | child_window(title="Windows (CR LF)", control_type="Text")
   |    | 
   |    | Static - 'UTF-8'    (L1714, T993, R1862, B1020)
   |    | ['UTF-8Static', 'Static7', 'UTF-8']
   |    | child_window(title="UTF-8", control_type="Text")
   |    | 
   |    | Static - 'INS'    (L1864, T993, R1900, B1020)
   |    | ['INS', 'INSStatic', 'Static8']
   |    | child_window(title="INS", control_type="Text")
   | 
   | Pane - ''    (L0, T54, R1920, B83)
   | ['Pane2']
   |    | 
   |    | Toolbar - ''    (L2, T54, R1920, B83)
   |    | ['Toolbar']
   |    |    | 
   |    |    | Button - ''    (L2, T54, R29, B80)
   |    |    | ['Button4']
   |    |    | 
   |    |    | Button - ''    (L29, T54, R56, B80)
   |    |    | ['Button5']
   |    |    | 
   |    |    | Button - ''    (L56, T54, R83, B80)
   |    |    | ['Button6']
   |    |    | 
   |    |    | Button - ''    (L83, T54, R110, B80)
   |    |    | ['Button7']
   |    |    | 
   |    |    | Button - ''    (L110, T54, R137, B80)
   |    |    | ['Button8']
   |    |    | 
   |    |    | Button - ''    (L137, T54, R164, B80)
   |    |    | ['Button9']
   |    |    | 
   |    |    | Button - ''    (L164, T54, R191, B80)
   |    |    | ['Button10']
   |    |    | 
   |    |    | Button - ''    (L199, T54, R226, B80)
   |    |    | ['Button11']
   |    |    | 
   |    |    | Button - ''    (L226, T54, R253, B80)
   |    |    | ['Button12']
   |    |    | 
   |    |    | Button - ''    (L253, T54, R280, B80)
   |    |    | ['Button13']
   |    |    | 
   |    |    | Button - ''    (L288, T54, R315, B80)
   |    |    | ['Button14']
   |    |    | 
   |    |    | Button - ''    (L315, T54, R342, B80)
   |    |    | ['Button15']
   |    |    | 
   |    |    | Button - ''    (L350, T54, R377, B80)
   |    |    | ['Button16']
   |    |    | 
   |    |    | Button - ''    (L377, T54, R404, B80)
   |    |    | ['Button17']
   |    |    | 
   |    |    | Button - ''    (L412, T54, R439, B80)
   |    |    | ['Button18']
   |    |    | 
   |    |    | Button - ''    (L439, T54, R466, B80)
   |    |    | ['Button19']
   |    |    | 
   |    |    | Button - ''    (L474, T54, R501, B80)
   |    |    | ['Button20']
   |    |    | 
   |    |    | Button - ''    (L501, T54, R528, B80)
   |    |    | ['Button21']
   |    |    | 
   |    |    | Button - ''    (L536, T54, R563, B80)
   |    |    | ['Button22']
   |    |    | 
   |    |    | Button - ''    (L563, T54, R590, B80)
   |    |    | ['Button23']
   |    |    | 
   |    |    | Button - ''    (L590, T54, R617, B80)
   |    |    | ['Button24']
   |    |    | 
   |    |    | Button - ''    (L617, T54, R644, B80)
   |    |    | ['Button25']
   |    |    | 
   |    |    | Button - ''    (L644, T54, R671, B80)
   |    |    | ['Button26']
   |    |    | 
   |    |    | Button - ''    (L671, T54, R698, B80)
   |    |    | ['Button27']
   |    |    | 
   |    |    | Button - ''    (L698, T54, R725, B80)
   |    |    | ['Button28']
   |    |    | 
   |    |    | Button - ''    (L725, T54, R752, B80)
   |    |    | ['Button29']
   |    |    | 
   |    |    | Button - ''    (L752, T54, R779, B80)
   |    |    | ['Button30']
   |    |    | 
   |    |    | Button - ''    (L787, T54, R814, B80)
   |    |    | ['Button31']
   |    |    | 
   |    |    | Button - ''    (L814, T54, R841, B80)
   |    |    | ['Button32']
   |    |    | 
   |    |    | Button - ''    (L841, T54, R868, B80)
   |    |    | ['Button33']
   |    |    | 
   |    |    | Button - ''    (L868, T54, R895, B80)
   |    |    | ['Button34']
   |    |    | 
   |    |    | Button - ''    (L895, T54, R922, B80)
   |    |    | ['Button35']
   |    |    | 
   |    |    | Button - ''    (L930, T54, R957, B80)
   |    |    | ['Button36']
   |    |    | 
   |    |    | Button - ''    (L957, T54, R984, B80)
   |    |    | ['Button37']
   |    |    | 
   |    |    | Button - ''    (L984, T54, R1011, B80)
   |    |    | ['Button38']
   |    |    | 
   |    |    | Button - ''    (L1011, T54, R1038, B80)
   |    |    | ['Button39']
   |    |    | 
   |    |    | Button - ''    (L1038, T54, R1065, B80)
   |    |    | ['Button40']
   |    |    | 
   |    |    | Button - ''    (L1065, T54, R1092, B80)
   |    |    | ['Button41']
   |    |    | 
   |    |    | Button - ''    (L1092, T54, R1119, B80)
   |    |    | ['Button42']
   |    |    | 
   |    |    | Button - ''    (L1119, T54, R1146, B80)
   |    |    | ['Button43']
   | 
   | TitleBar - ''    (L20, T-6, R1920, B29)
   | ['TitleBar2']
   |    | 
   |    | Menu - '系统'    (L0, T0, R22, B22)
   |    | ['Menu', '系统Menu', '系统', '系统0', '系统1', 'Menu0', 'Menu1']
   |    | child_window(title="系统", auto_id="MenuBar", control_type="MenuBar")
   |    |    | 
   |    |    | MenuItem - '系统'    (L0, T0, R22, B22)
   |    |    | ['MenuItem', '系统2', '系统MenuItem', 'MenuItem0', 'MenuItem1']
   |    |    | child_window(title="系统", control_type="MenuItem")
   |    | 
   |    | Button - '最小化'    (L1740, T0, R1800, B28)
   |    | ['最小化', '最小化Button', 'Button44']
   |    | child_window(title="最小化", control_type="Button")
   |    | 
   |    | Button - '还原'    (L1800, T0, R1859, B28)
   |    | ['还原Button', 'Button45', '还原']
   |    | child_window(title="还原", control_type="Button")
   |    | 
   |    | Button - '关闭'    (L1859, T0, R1920, B28)
   |    | ['关闭3', '关闭Button3', 'Button46']
   |    | child_window(title="关闭", control_type="Button")
   | 
   | Menu - '应用程序'    (L0, T23, R1536, B42)
   | ['Menu2', '应用程序Menu', '应用程序']
   | child_window(title="应用程序", auto_id="MenuBar", control_type="MenuBar")
   |    | 
   |    | MenuItem - '文件(F)'    (L0, T23, R53, B42)
   |    | ['文件(F)', '文件(F)MenuItem', 'MenuItem2']
   |    | child_window(title="文件(F)", control_type="MenuItem")
   |    | 
   |    | MenuItem - '编辑(E)'    (L53, T23, R106, B42)
   |    | ['编辑(E)', '编辑(E)MenuItem', 'MenuItem3']
   |    | child_window(title="编辑(E)", control_type="MenuItem")
   |    | 
   |    | MenuItem - '搜索(S)'    (L106, T23, R160, B42)
   |    | ['搜索(S)', '搜索(S)MenuItem', 'MenuItem4']
   |    | child_window(title="搜索(S)", control_type="MenuItem")
   |    | 
   |    | MenuItem - '视图(V)'    (L159, T23, R213, B42)
   |    | ['视图(V)', '视图(V)MenuItem', 'MenuItem5']
   |    | child_window(title="视图(V)", control_type="MenuItem")
   |    | 
   |    | MenuItem - '编码(N)'    (L214, T23, R270, B42)
   |    | ['编码(N)MenuItem', 'MenuItem6', '编码(N)']
   |    | child_window(title="编码(N)", control_type="MenuItem")
   |    | 
   |    | MenuItem - '语言(L)'    (L270, T23, R323, B42)
   |    | ['语言(L)', '语言(L)MenuItem', 'MenuItem7']
   |    | child_window(title="语言(L)", control_type="MenuItem")
   |    | 
   |    | MenuItem - '设置(T)'    (L322, T23, R376, B42)
   |    | ['设置(T)MenuItem', 'MenuItem8', '设置(T)']
   |    | child_window(title="设置(T)", control_type="MenuItem")
   |    | 
   |    | MenuItem - '工具(O)'    (L376, T23, R432, B42)
   |    | ['工具(O)MenuItem', '工具(O)', 'MenuItem9']
   |    | child_window(title="工具(O)", control_type="MenuItem")
   |    | 
   |    | MenuItem - '宏(M)'    (L432, T23, R478, B42)
   |    | ['宏(M)', '宏(M)MenuItem', 'MenuItem10']
   |    | child_window(title="宏(M)", control_type="MenuItem")
   |    | 
   |    | MenuItem - '运行(R)'    (L478, T23, R532, B42)
   |    | ['运行(R)MenuItem', '运行(R)', 'MenuItem11']
   |    | child_window(title="运行(R)", control_type="MenuItem")
   |    | 
   |    | MenuItem - '插件(P)'    (L533, T23, R587, B42)
   |    | ['插件(P)', '插件(P)MenuItem', 'MenuItem12']
   |    | child_window(title="插件(P)", control_type="MenuItem")
   |    | 
   |    | MenuItem - '窗口(W)'    (L586, T23, R644, B42)
   |    | ['窗口(W)', '窗口(W)MenuItem', 'MenuItem13']
   |    | child_window(title="窗口(W)", control_type="MenuItem")
   |    | 
   |    | MenuItem - '?'    (L645, T23, R665, B42)
   |    | ['?', '?MenuItem', 'MenuItem14']
   |    | child_window(title="?", control_type="MenuItem")
   |    | 
   |    | MenuItem - 'X'    (L1514, T23, R1536, B42)
   |    | ['X', 'XMenuItem', 'MenuItem15']
   |    | child_window(title="X", control_type="MenuItem")

Process finished with exit code 0

从上边的内容中,找到【首选项...】和【关闭】备用。

3.3代码设计

3.3参考代码

# -*- coding:utf-8 -*-

# 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行

# 2.注释:包括记录创建时间,创建人,项目名称。
'''
Created on 2025-05-12
@author: 北京-宏哥
北京宏哥(微信搜索:北京宏哥,关注宏哥,提前解锁更多测试干货!)
Project: PC端自动化测试实战教程-11-pywinauto操作弹出新窗口上按钮(详细教程)
'''

# 3.导入模块
from pywinauto import Application
import time

app = Application('uia').start("D:/software/Notepad/Notepad++/notepad++.exe")
win = app.window(title='新文件 1 - Notepad++')
# 操作级联菜单
win.menu_select('设置(T) -> 首选项...')
time.sleep(5)
win.print_ctrl_ids()
# 切换到界面窗口
pref_win = win.child_window(title="首选项", control_type="Window")
# 点关闭按钮
pref_win.child_window(title="关闭", auto_id="6001", control_type="Button").click()
win.type_keys("北京-宏哥")

3.4运行代码

1.运行代码,右键Run'Test',就可以看到控制台输出,如下图所示:

2.运行代码后电脑端的动作(和宏哥所描述的测试场景一致)。如下图所示:

 4.小结

 4.1常见问题

    • ‌backend选择‌
      若控件无法识别,尝试切换backend="win32"(适用于传统Win32应用)

    • ‌标题模糊匹配‌
      使用title_re=".*部分标题.*"正则表达式应对动态标题变化

    • ‌跨进程弹窗‌
      对于独立进程弹窗,需通过Application().connect(process=pid)重新建立连接

  好了,时间不早了今天就分享到这里,感谢你耐心地阅读!