Pywinauto无法识别Chrome打印窗口UI元素问题求助
问题描述
使用Selenium访问网页、导航至指定页面并触发打印对话框,计划通过Pywinauto操控该对话框设置打印选项并打印文档,但无法定位对话框内的UI元素。Inspect.exe可正常定位目标元素,但Pywinauto连接Chrome窗口后找不到所需控件。
执行代码
from pywinauto.application import Application app = Application(backend="uia").connect(title_re=".*- Google Chrome -.*") win = app.window(best_match="using pywinauto, how can I draw a rectangle arround a ui element?") win.print_control_identifiers()
控件标识符输出片段
| Pane - 'Print' (L299, T78, R1622, B955) | ['Print', 'PrintPane', 'Pane22'] | child_window(title="Print", control_type="Pane") | | | | Pane - '' (L299, T78, R1622, B955) | | ['Pane23'] | | | | | | Pane - '' (L299, T78, R1622, B955) | | | ['Pane24'] | | | | | | | | Pane - '' (L0, T0, R0, B0) | | | | ['Pane25'] | | | | | | | | Pane - '' (L306, T83, R1615, B946) | | | | ['Pane26']
解决方向
- 直接定位打印对话框窗口:Chrome打印对话框是独立顶层窗口,不要通过Chrome主窗口嵌套查找,直接定位标题为
Print的窗口:print_dialog = app.window(title="Print", control_type="Window") print_dialog.print_control_identifiers() - 更新Pywinauto并检查兼容性:新版本Chrome的UI控件结构可能变化,执行
pip install --upgrade pywinauto更新至最新版,确保与当前Chrome版本兼容。 - 切换backend尝试:若
uiabackend无效,切换为win32backend测试,部分Chrome控件对win32支持更友好。 - 用Inspect.exe获取精确属性定位:在Inspect.exe中提取目标元素的
AutomationId或Name,直接通过属性定位控件,例如:# 示例:通过AutomationId定位打印目标下拉框 dropdown = print_dialog.child_window(automation_id="DestinationSelectDropdown", control_type="ComboBox") - 修正窗口匹配逻辑:原代码中
best_match使用网页标题,可能匹配到Chrome的网页内容窗口而非打印对话框,优先用标题、控件类型等精确属性定位。
内容的提问来源于stack exchange,提问作者osantos
相关产品推荐
相关产品推荐

