无法在用户提供“路径”作为输入后自动执行各个关键操作。

huangapple go评论66阅读模式
英文:

Unable to automate the individual key actions after providing 'path' as user input

问题

Initially,我的代码在将path/to/file直接分配给变量并将该变量作为参数传递给函数时正常工作。我的工作代码:

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)

那么它会正常工作。我无法弄清楚为什么会发生这种情况以及如何修复此问题,因为在步骤之间包含延迟并不是理想的解决方案。是否有人能帮忙解决?非常感谢!

英文:

Initially my code worked fine when the path/to/file was assigned to a variable directly and this variable was passed as an argument to a function. My working code:

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)

Then I modified my code (all the necessary imports are made correctly) so that the path is provided as user input by opening a dialogue box:

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)

Here, I got an error pywinauto.findwindows.ElementNotFoundError: {'title': 'Check installation package', 'backend': 'win32', 'process': 22936}. However, it was possible to run the code without error if the {TAB 3} was split into 3.
For example, when the code was modified to:

app.MyTestApplication.type_keys("{TAB}")
time.sleep(1)
app.MyTestApplication.type_keys("{TAB}")
time.sleep(1)
app.MyTestApplication.type_keys("{TAB}")
time.sleep(1)

then it was working. I could not figure out why this is happening and how to fix this issue, since having to include delay between steps is not an ideal solution. Could someone please help? Many thanks in advance!

答案1

得分: 1

.type_keys()方法的文档应该描述按键之间的时间间隔参数。此外,在一个字符串的操作序列中可以使用{PAUSE 1.0}。最好的方式是使用其他方法(如.invoke().select(...)等)来执行操作,而不是通常情况下的type_keysclick_input

即使在GUI中某些操作看起来瞬间完成,实际上还是需要一些时间(也许不到1秒,但仍然需要)。这种时间问题可以通过适当的等待操作来处理:请参阅等待长时间操作指南。

英文:

.type_keys() method documentation should describe parameter for time interval between key presses. Also it's possible to use {PAUSE 1.0} inside the sequence of actions in one string. And finally the best way is doing actions by some other methods (.invoke(), .select(...) etc.) except type_keys and click_input which are the last hope usually.

Also even if some action in GUI looks instantaneous, it takes some time (maybe less than 1 sec. but still). And such timing issues can be handled by proper wait actions: see Waiting for Long Operations guide.

huangapple
  • 本文由 发表于 2023年4月20日 05:01:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定