pythonautomationpywinauto

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


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!


Solution

  • .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.