最初,当路径/到/文件直接分配给变量并且该变量作为参数传递给函数时,我的代码工作正常。我的工作代码:
def read_installation_package(filePath):
...
...
dlg = app.window(class_name="#32770")
dlg.Edit.type_keys(filePath, with_spaces = True)
dlg.Open.click()
app.MyTestApplication.type_keys("{TAB 3}")
...
...
chkwin = app.window(title="Check installation package")
...
...
def main():
file_path = "C:\\Development Projects\\installation-files.zip"
read_installation_package(file_path)
然后我修改了我的代码(正确地进行了所有必要的导入),以便通过打开对话框将路径提供为用户输入:
import tkinter as tk
from tkinter import filedialog
def read_installation_package(filePath):
...
...
dlg = app.window(class_name="#32770")
dlg.Edit.type_keys(filePath, with_spaces = True)
dlg.Open.click()
app.MyTestApplication.type_keys("{TAB 3}")
...
...
chkwin = app.window(title="Check installation package")
...
...
def main():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
file_path = file_path.replace("/", "\\")
# file_path = "C:\\Development Projects\\installation-files.zip"
read_installation_package(file_path)
在这里,我得到一个错误
pywinauto.findwindows.ElementNotFoundError: {'title': 'Check installation package', 'backend': 'win32', 'process': 22936}
。但是,如果将 {TAB 3} 拆分为 3,则可以无错误地运行代码。
比如当代码修改为:
app.MyTestApplication.type_keys("{TAB}")
time.sleep(1)
app.MyTestApplication.type_keys("{TAB}")
time.sleep(1)
app.MyTestApplication.type_keys("{TAB}")
time.sleep(1)
然后它开始工作了。我无法弄清楚为什么会发生这种情况以及如何解决此问题,因为必须在步骤之间包括延迟并不是一个理想的解决方案。有人可以帮忙吗?非常感谢!