1

I have a piece of pywinauto code which attempts to delete an Item from an ItemList. Occasionally the type_keys() isn't working, as if the window is going out of focus. Interestingly enough, if I click on the Command Prompt window where the python code is running, this suddenly triggers the code to properly set focus on the application and continue running.

Here's an overview of the steps:

  1. Opens an Open Diaglog window in certain application
    (here its Tradestation's Development Environment)
  2. Selects an item from the ItemList
  3. Type DELETE key (confirmation window pops up)
  4. Select 'Yes' in the confirmation window

Most of the time this sequence works, except occasionally (maybe 1 out of every 5 or so tries) it fails where the DELETE key press isn't happening. The item is selected, but the window appears out of focus (or so it seems).

I've been chasing this issue for days/weeks, trying various different things, i.e. added delays, adjusted timeouts, adjusted pywinauto.Timings.fast/slow, tried setting focus in various places, etc... I also put the code into an infinite 'while' loop hoping that it would work on subsequent tries.

Interestingly enough, when it gets stuck, if I click on the Windows Command Prompt window where the python application is running, then suddenly the Open Diaglog box goes back into focus and continues to run successfully.

I'm looking for suggestions as to the root cause OR what else I could try.

Here's where the code has ended up:

strategy = self.window.child_window(title=name, control_type='ListItem')
Timings.slow()
while True:
    # Issues where delete/confirm window not showing
    # Try solving with SetFocus in case the open diag is going out of focus
    _logger.info("Set focus")
    handle = win32gui.FindWindow(None, OPEN_DLG_TITLE)
    win32gui.SetForegroundWindow(handle)

    # Highlight the strategy
    _logger.info("Select Strategy")
    strategy.select()
    if strategy.is_selected() == 0:
        time.sleep(2)
        continue
    _logger.info("Strategy selected")

    # Delete it...
    _logger.info("Type DELETE")
    #strategy.type_keys('{DELETE}')                
    strategy.type_keys('{DELETE}', set_foreground=False)

    # Delay in case return comes back too quickly
    time.sleep(2)

    # Click 'Yes' in Confirmation Window
    _logger.info("Check confirmation window")
    confirm_window = self.window.child_window(
        title="Confirm file removal",
        control_type="Window"
    )
    if confirm_window.exists() is False:
        try:
            _logger.info('... does not exist.  Briefly wait 5 seconds')
            confirm_window.wait('exists', timeout=5)
        except pywinauto.timings.TimeoutError:
            #breakpoint()
            _logger.info('---- Retrying -----')
            continue
    assert confirm_window.exists() is True

    break
Timings.fast()

jersey bean
  • 3,321
  • 4
  • 28
  • 43

0 Answers0