Regarding pywinauto

Asked 2 years ago, Updated 2 years ago, 22 views

#coding:utf-8
from datetime import datetime
from pywinauto import application
app=application.Application.start("notepad.exe")

app.Notepad.Edit1.SetText(unicode(datetime.now()))
app.Notepad.MenuSelect(u "File - > Save As")
dialog = app [u "Save As" ]
dialog.Edit1.SetText(u"datetime.txt")
dialog.Button1.Click()

confirm=app[u "Confirm Save As"]
if confirm.Exists():
    confirm.Button1.Click()


app.Notepad.TypeKeys ("%FX")

The following error appears:

Unicode equal comparison failed to convert both arguments to Unicode-interpreting them as being unique
  a[besti+bestsize] == b[bestj+bestsize]:
Traceback (most recent call last):
  File "L:/tools/Python/pyato/notepad.py", line 7, in <module>
    app.Notepad.MenuSelect(u "File - > Save As")
  File "C:\Python27\lib\site-packages\pywinauto\controls\HwndWrapper.py", line 1316, in MenuSelect
    self.MenuItem(path, exact=exact).Select()
  File "C:\Python27\lib\site-packages\pywinauto\controls\HwndWrapper.py", line 1266, in MenuItem
    return self.Menu().GetMenuPath(path,appdata=menu_appdata,exact=exact)[-1]
  File "C:\Python27\lib\site-packages\pywinauto\controls\menuwrapper.py", line 470, in GetMenuPath
    self.Items())
  File "C:\Python27\lib\site-packages\pywinauto\findbestmatch.py", line 139, in find_best_match
    raise MatchError (items=text_item_map.keys(), tofind=search_text)
pywinauto.findbestmatch.MatchError   

Is Unicode strange?
I am a beginner in the program, so I cannot find a solution.
Would you mind teaching me?
Python is 2.79 pywinauto is 0.41.
I apologize for the inconvenience, but I appreciate your cooperation.

python

2022-09-30 19:50

1 Answers

pywinauto/controlls/menuwrapper.py uses win32gui.GetMenuItemInfo, but I don't think that function corresponds to unicode.

If you check old version of pywinauto's code, GetMenuItemInfo of pywinauto/win32functions.py is used.GetMenuItemInfo is used in the file win32 functions.Sending unicode .GetMenuItemInfoW to the corresponding function.

Replacing _read_item and Text in the MenuItem class with old code seems to work:

#coding:utf-8
import ctypes
from datetime import datetime
from pywinauto import application
from pywinauto.controls.menuwrapper import MenuItem, win32 structures, win32 functions, win32 definitions


def_read_item(self):
    """Read the menu item info""

    See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/menus/menuferencing/menufunctions/getmenuiteminfo.asp
    US>"for more information."
    menu_info=win32 structures.MENUITEMINFOW()
    menu_info.cbSize=ctypes.sizeof(menu_info)
    menu_info.fMask=\
        win32 define.MIIM_CHECKMARKS|\
        win32 define.MIIM_ID|\
        win32 define.MIIM_STATE|\
        win32 define.MIIM_SUBMENU|\
        win32defines.MIIM_TYPE#|\
        #MIIM_FTYPE#|\
        #MIIM_STRING
        #MIIM_DATA|\

    ret = win32 functions.GetMenuItemInfo(
        self.menu,
        self.index,
        True,
        ctypes.byref(menu_info))

    if not ret:
        raise types.WinError()

    return menu_info

def Text (self):
    "Return the state of this menu item".

    info=self._read_item()
    # if there is text
    if info.cch:
        # allocate buffer
        buffer_size=info.cch+1
        text=ctypes.create_unicode_buffer(buffer_size)

        # update the structure and get the text info
        info.dwTypeData=ctypes.addressof(text)
        info.cch=buffer_size

        win32 functions.GetMenuItemInfo(
            self.menu,
            self.index,
            True,
            ctypes.byref(info))

        text = text.value
    else:
        text = '

    return text
MenuItem._read_item=_read_item
MenuItem.Text=Text


app=application.Application.start("notepad.exe")

app.Notepad.Edit1.SetText(unicode(datetime.now()))
app.Notepad.MenuSelect(u "File - > Save As")
dialog = app [u "Save As" ]
dialog.Edit1.SetText(u"datetime.txt")
dialog.Button1.Click()

confirm=app[u "Confirm Save As"]
if confirm.Exists():
    confirm.Button1.Click()


app.Notepad.TypeKeys ("%FX")

So it looks like a unicode bug in a module called pywinauto.


2022-09-30 19:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.