很多朋友在使用pywinauto的时候会遇到其无法对中文应用的菜单等操作的问题,其实这只是由于编码引起的问题。
解决这个问题有两种方法:
第一种方法是使用"u"讲字符串转换成UTF格式的字符串:
…………
EDIT_NAME = u'保存网页'
SAVE_NAME = u'保存(&S)'
ASK_NAME = u'保存网页'
YES_NAME = u'是(&Y)'
…………
app = Application().start_(r"c:program filesinte.net exploreriexplore.exe %s"% web_addresss)
time.sleep(1)
ie = app.window_(title_re = ".*Microsoft Internet Explorer.*")
print "No Menu's in IE:", ie.MenuItems()
print "They are implemented as a toolbar:", ie.Toolbar3.Texts()
ie.TypeKeys("%FA")
SaveWebPage = app[EDIT_NAME]
SaveWebPage['Edit'].SetEditText(os.path.join(r"c:.temp",outputfilename))
…………
第二种则是使用decode函数强行转换字符串的编码:
…………
CP = 'cp936'
EDIT_NAME = '保存网页'.decode(CP)
SAVE_NAME = '保存(&S)'.decode(CP)
ASK_NAME = '保存网页'.decode(CP)
YES_NAME = '是(&Y)'.decode(CP)
…………
app = Application().start_(r"c:program filesinternet exploreriexplore.exe %s"% web_addresss)
time.sleep(1)
ie = app.window_(title_re = ".*Microsoft Internet Explorer.*")
print "No Menu's in IE:", ie.MenuItems()
print "They are implemented as a toolbar:", ie.Toolbar3.Texts()
ie.TypeKeys("%FA")
SaveWebPage = app[EDIT_NAME]
SaveWebPage['Edit'].SetEditText(os.path.join(r"c:.temp",outputfilename))
…………