python - 使用 pywinauto 按标题选择窗口
问题描述
使用 pywinauto 我试图在软件出现时关闭一个弹出窗口,因为它是有条件的。
def get_window():
app = pywinauto.application.Application(backend="uia")
app.connect(path='gofer.exe')
#app.Properties.print_control_identifiers()
trade = app.window(best_match='Warning:')
# trade.wrapper_object().close
print(trade)
if trade == 'Warning:':
print("You see the Window")
# press enter key to displace
# start next action
else:
print("Naw, No window bro")
# Log to file
pass
print(trade) 的输出是:
<pywinauto.application.WindowSpecification object at 0x0000019B8296DBA8>
所以我知道它至少可以工作,但不会去我想要的地方。警告是一个弹出的窗口,根据 spy++,其标题为“警告”。
但是,我无法打印窗口数据......虽然窗口是一个弹出窗口,但如果这有所不同,它就不是一个 toast 弹出窗口。这是一个dlg窗口。
属性打印一个仅引用主程序并提示对话窗口的字典,但从不指定属性。即使在搜索主程序标题时,我也无法使其正常工作。
解决方案
这就是我为识别弹出窗口所做的工作。本质上,您想创建一个 Dialog (dlg) 表示作为弹出窗口的父窗口的窗口:
app = pywinauto.application.Application(backend="uia")
app.connect(path='gofer.exe')
# Using regular expression to create a dialog of the gofer.exe app
# I am assuming the title will match "*Gofer*" eg: "Gofer the Application"
dlg = app.window(title_re=".*Gofer.*")
# Now I am going to identify the title of the popup window:
dlg.print_ctrl_ids()
# If you did the last step correctly, the output will look something like:
#Control Identifiers:
#Dialog - 'Gofer The Application' (L688, T518, R1065, B1006)
#[u'Dialog', u'Gofer Dialog']
#child_window(title="Warning: ", control_type="Window")
#|
#| Image - '' (L717, T589, R749, B622)
#| [u'', u'0', u'Image1', u'Image0', 'Image', u'1']
#| child_window(auto_id="13057", control_type="Image")
#|
#| Image - '' (L717, T630, R1035, B632)
#| ['Image2', u'2']
#| child_window(auto_id="13095", control_type="Image")
#|
# Now using the same title and control type for the popup that we identified
# We check to see if it exists as follows:
if dlg.child_window(title="Warning:", control_type="Window").exists():
print("Bro, you got a pop-up bro...")
else:
print("No popup Bro...")
推荐阅读
- java - MailJob 中附加的图像 - Android 工作室
- html - 页眉中的文字定位,页脚定位,
- node.js - 如何使用 typescript 在 node/mysql2 中包装 pool.query 函数以进行错误处理
- python - 关于寻找“毕达哥拉斯三胞胎”的脚本的两个问题
- php - 从安全的角度来看,在从服务器请求信息的同时直接通过 url 发送 http 请求是否可取?
- jquery - Jquery迭代未显示所有列
- javascript - 当可以使用工具提示禁用一个按钮时如何创建拆分按钮?
- bash - bash 循环读取 txt 文件中的变量并对其进行处理
- servlets - 如何将我的 servlet 连接到另一个类的布尔方法?
- python - 正方体训练特定字符