1

I have a python script that opens an application and then sets focus to that application using the pywinauto module:

#Module for starting app + controlling app window
from pywinauto.application import Application
from pywinauto.controls.hwndwrapper import HwndWrapper

#Module for delay
import time

#Application path
command="C:\Program Files\Basler Electric\BESTCOMSPlus\BESTCOMSPlusShell.exe"
#command="C:\Program Files\Internet Explorer\iexplore.exe"

app = Application()
app.start(command)
time.sleep(10)
HwndWrapper(app.BEST.Edit.handle).set_focus()
#HwndWrapper(app.IEFrame.Edit.handle).set_focus()

I originally tested this code with internet explorer (as seen in the two commented out lines) and it worked correctly. Now I have switched to the actual application I would like to open and I'm getting the following error:

File "C:\Python36\lib\site-packages\pywinauto-0.6.7-py3.6.egg\pywinauto\findbestmatch.py", line 533, in find_best_control_matches
    raise MatchError(items = name_control_map.keys(), tofind = search_text)
pywinauto.findbestmatch.MatchError: Could not find 'BEST' in 'dict_keys(['BESTCOMSPlus®WindowsForms10.Window.8.app.0.1ed9395_r6_ad1', 'WindowsForms10.Window.8.app.0.1ed9395_r6_ad1', 'BESTCOMSPlus®'])'

If I try using a longer string for matching, say BESTCOMSPlus, I get this error where it seems to think that I'm trying to match Edit:

File "C:\Python36\lib\site-packages\pywinauto-0.6.7-py3.6.egg\pywinauto\findbestmatch.py", line 533, in find_best_control_matches
    raise MatchError(items = name_control_map.keys(), tofind = search_text)
pywinauto.findbestmatch.MatchError: Could not find 'Edit' in 'dict_keys(['', 'WindowsForms10.MDICLIENT.app.0.1ed9395_r6_ad1', '0', '1', '2', 'WindowsForms10.Window.8.app.0.1ed9395_r6_ad1', '3', 'WindowsForms10.Window.8.app.0.1ed9395_r6_ad10', 'WindowsForms10.Window.8.app.0.1ed9395_r6_ad11', 'WindowsForms10.Window.8.app.0.1ed9395_r6_ad12', '4', 'WindowsForms10.Window.8.app.0.1ed9395_r6_ad13', '5', 'WindowsForms10.Window.8.app.0.1ed9395_r6_ad14', '6', 'WindowsForms10.Window.8.app.0.1ed9395_r6_ad15'])'

Based on this question I understand that I'm using the best_match function implicitly, which I thought would return either the BESTCOMSPlus® or BESTCOMSPlus®WindowsForms10.Window.8.app.0.1ed9395_r6_ad1 window.

Is this an issue due to two windows having the same string as part of their name? And why does using a longer string give me this error?

For the record my Python version is 3.6.7 and I've been testing in IDLE

bdkong
  • 181
  • 3
  • 9
  • 22
  • For WinForms app I would recommend using `Application(backend="uia")` and then `app.window(title_re="BESTCOMSPlus.*").Edit.set_focus()` or `.set_keyboard_focus()` – Vasily Ryabov Nov 20 '19 at 11:01

0 Answers0