pywinauto change application name

1.4k Views Asked by At

My project revolves around automating a few task. I open an application, a new 3D file then I do some operation from an access base. I chose pywinauto and graphic interaction because of the operator needs to do intervention on the software.

My issue is that: the name of the application change when I open a new file. There is the code :

# Search data on the Access Base
cursor = conn.cursor()
cursor.execute("SELECT * FROM [SophieBot_produit] WHERE [SophieBot_produit].[ID PRODUCTION] =?",(prod))
produit_sql = cursor.fetchall()
production, statut, produit_value, nb_element, cas, gamme, sl_tps_open, sl_tps_gen, sl_tps_save = produit_sql[0]
produit = str(produit_value).zfill(8)

  # Data work, check with print()

#Connecting to the software
app = application.Application()
app.Connect(path='C:\\myprogram')

  # First step everything works
app.SLMAutoFab.menu_select("File -> Open")
app.window_(title_re="Open file.*").Edit.SetEditText('S:\\32 - Articles\\'+ str(gamme) +'\\' + str(cas) +'\\' + str(produit) +'_sup.stl')
app.window_(title_re="Open file.*").Ouvrir.Click()

  # New step is to do operation on the  software

Now my software application name is changing Myprogram - 3DFile

When you write the new name everything works for example :

app.SLMAutoFab00057389_sup.Part.Click()

But the name of the software change, so I try :

win = 'MyProgram' + str(produit)
app.win.Part.Click()

The code doesn't work and the error message is:

MatchError: Could not find 'win' in 'dict_keys

I'm trying to connect with window(handle), and will update the post if it works. Any hints or pointers are greatly appreciated.

3

There are 3 best solutions below

1
On

You can use a regular expression to match part of the window title.

Check out the title_re attribute when calling app.window().

Documentation: Window specification

This example matches all windows with a title that starts with "SMLAutoFab":

w = app.window(title_re='SMLAutoFab.*')
w.Edit.SetEditText('S:\\32 - Articles\\'+ str(gamme) +'\\' + str(cas) +'\\' + str(produit) +'_sup.stl')
1
On

There is the error @Jens

  File "link\Bot.py", line 99, in slicing
    win.Part.Click() 
  File "C:\link\site-packages\pywinauto\application.py", line 351, in __getattribute__
    ctrls = self.__resolve_control(self.criteria)
  File "C:\link\site-packages\pywinauto\application.py", line 248, in __resolve_control
    raise e.original_exception
  File "C:\link\site-packages\pywinauto\timings.py", line 425, in wait_until_passes
    func_val = func(*args)
  File "C:\link\site-packages\pywinauto\application.py", line 190, in __get_ctrl
    dialog = self.backend.generic_wrapper_class(findwindows.find_element(**criteria[0]))
  File "C:\link\site-packages\pywinauto\findwindows.py", line 84, in find_element
    elements = find_elements(**kwargs)
  File "C:\link\site-packages\pywinauto\findwindows.py", line 300, in find_elements
    elements = findbestmatch.find_best_control_matches(best_match, wrapped_elems)
  File "C:\link\findbestmatch.py", line 533, in find_best_control_matches
    raise MatchError(items = name_control_map.keys(), tofind = search_text)
MatchError: Could not find 'SLMAutoFab.*' in 'dict_keys(['Programblabla']

0
On

Ok, I find it. It was stupid and the inspiration came from neuhaus.

I just put, but with the exact name, I forgot a space...:

win = app.window(title=name_sup)
win.Part.Click()