首页 > 解决方案 > 使用 pywinauto 更改工具栏中的文本

问题描述

我有一个来自我试图自动化的 Windows 应用程序的文件选择窗口。

在此处输入图像描述

我可以使用更改底部的文件名框

app2.window(title_re='Select a batch file') \
            .File_name_Edit.set_text(selected_filename)

我之前在下部框中输入了完整的文件路径和文件名,但是,在 Windows 更新后,它将不再接受文件名框中的斜杠。因此问题。

所以问题是顶部(目录/文件夹名称)框。

手动我可以选择该框,突出显示它然后改写目录。

我找不到使用 pywinauto 的方法。

尝试的方法是:

toolbar2 = app2.window(title_re='Select a batch file')  # \
#     .child_window(title_re='Address', auto_id='1001')
#     .child_window(title_re='Address', control_type="ToolBar", auto_id='1001')
file_path_address = toolbar2['Address band toolbarToolbar'].click_input()
file_path_address.set_text(directory_path)

# ToolbarWrapper(toolbar2).set_text(directory_path)
# ToolbarWrapper(toolbar2).click(button='All locationsSplitButton').set_text(directory_path)
# ToolbarWrapper(toolbar2)['Address:'].set_text(directory_path)

我正在使用 set_text,因为自动化必须在锁定屏幕后运行,type_keys 不适用于锁定屏幕,因为 type_keys 是一种键盘方法,并且当屏幕锁定时 Windows 会阻止键盘输入。

我无权访问正在自动化的程序的内部以更改原始目录路径,并且需要将其更改为从不同位置读取。

任何援助将不胜感激。

标签: toolbarpywinauto

解决方案


请参见下面的代码,它在 Win 10 上使用 Notepad++ 运行良好

import time
from pywinauto.application import Application

app = Application().Connect(title=u'Open', class_name='#32770')
window = app.Dialog

toolbarwindow = window.Toolbar3
toolbarwindow.Click()
time.sleep(0.30)

static = window.Edit2
directory_path = "C:\Users\Desktop"

raw_directory_path = r'{}'.format(directory_path)
static.set_edit_text(text = raw_directory_path)
static.type_keys("{ENTER}")

推荐阅读