Search code examples
pythonautomationpywinauto

Pywinauto automation fails when there is no user session


I am working on program automation (program named SEO indexer). I wrote the automation using python's library name pywinauto. Everything works just great when I am running the automation over RDP connection to the server. But when I am trying to leave the program and disconnect from RDP the "Save AS" windows window is not starting and the program crashes ...

someone knows how can I fix it?

the code that is responsible to save the file is -

def run(self, process_id, link):
    controls = self._app[u'TForm1']

    text_box = controls.Edit 
    text_box.set_text(link)

    button = controls.ToolBar1

    windows = Desktop(backend="uia")

    button.click()

    self.wait_for_finish(process_id)

    result_box = controls.TVirtualStringTree 
    result_box.RightClick()

    sleep(1)

    windows_list = windows.windows()
    context_menu = windows.window(best_match = "Context")
    save_all_button = context_menu.children()[2]
    save_all_button.select()
    save_as = windows.window(best_match = "save_as")
    properties_section = save_as.children()[0]

    file_name = "C:\\Windows\\Temp\\indexer_" + str(randint(0, 10000))
    file_name_label = properties_section.children()[4].children()[0]
    file_name_label.set_text(file_name)

    save_button = save_as.children()[2]
    save_button.click()

    sleep(2)

    yes_no_dialog  = windows.window(best_match = "GSA SEO Indexer v2.34")
    yes_no_dialog.children()[0].click()

    return file_name

it crashed on - save_as = windows.window(best_match = "save_as")

there is a way to force it opening the save as dialog even if there are no screen ?

UPDATE:

I Just notices that the problem is not that the Save as panel is not created, the problem is that when I am without screen and trying to select from the context menu (which is created) - just the text is selected, without clicking on it


Solution

  • The Remote Execution Guide is what you need. This is common problem for any GUI automation tool. So the question is not exactly pywinauto related, but I wrote this guide a year ago to address it for many users.