Appium windows+python3+appium1.4.16+android6.0,appium server 中文显示乱码求解?

蓝莓酱 · 2017年06月28日 · 最后由 song 回复于 2018年07月25日 · 2288 次阅读

windows+python3+appium1.4.16+android6.0,appium server 中文显示乱码,如图:

在论坛上查了一下,有相关帖子,按照操作尝试,还是未果。
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

然后又查了一下,说是 python2.7 以后不用 setdefaultencoding 了。但我的显示依旧是乱码。
有些帖子还说,这么解决编码问题会产生隐形的 bug,但是刚入门的时候可能都会这么解决。
所以想看看已经解决这个问题的前辈们,最终的解决办法是什么呢?

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 10 条回复 时间 点赞

把 log 存成文件看

谢谢你的回答,但是我觉得肯定还有解决办法

当然有,所有 python 内部处理用 str(py3)/unicode(py2),所有输出转换到对应编码

Appium 版本太老了把

fdeferf 回复

我也是刚入这块,不是很了解,我看说是这个版本比较稳定,所以就用啦

很感谢你的回复,但是能否具体说说解决办法呢?
下面是我 pycharm 的设置。

因为到现在为止,我还没解决掉这个问题,所以请教下前辈们

我看了一下,只有 xpath 有中文的时候,会乱码,xpath 中可以添加 u 么?

python3 不用添加 u
IDE/Project 的编码和你 terminal 的编码没关系.
举个例子,如果你用的是中文版 windows,那大多数情况下你 cmd 的编码是 gbk,所以当你需要 print 东西到 cmd 之前,需要转成 gbk
至于你现在看到的窗口,看上去是 appium 起的,你得查看起的这个 terminal 的输出编码是什么

例如,我通过 xpath 查找这个元素:self.el.get_xpath("//android.widget.CheckedTextView[@text='成员管理']")

在 appium--server 输出结果:

info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:click","params":{"elementId":"1"}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: click
info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":true}
info: [debug] Responding to client with success: {"status":0,"value":true,"sessionId":"2ccceab6-d086-4394-a095-ab2e7d9efd0e"}
info: <-- POST /wd/hub/session/2ccceab6-d086-4394-a095-ab2e7d9efd0e/element/1/click 200 271.960 ms - 76 {"status":0,"value":true,"sessionId":"2ccceab6-d086-4394-a095-ab2e7d9efd0e"}
info: --> POST /wd/hub/session/2ccceab6-d086-4394-a095-ab2e7d9efd0e/element {"using":"xpath","value":"//android.widget.TextView[@text='鑼冨畞瀹?]","sessionId":"2ccceab6-d086-4394-a095-ab2e7d9efd0e"}
info: [debug] Waiting up to 5000ms for condition
info: [debug] Pushing command to appium work queue: ["find",{"strategy":"xpath","selector":"//android.widget.TextView[@text='鑼冨畞瀹?]","context":"","multiple":false}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//android.widget.TextView[@text='鑼冨畞瀹?]","context":"","multiple":false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding //android.widget.TextView[@text='鑼冨畞瀹?] using XPATH with the contextId: multiple: false

都是 xpath 的位置,有中文就显示论码。
我的 pycharm 设置的是 utf-8。
我的代码开头也用 # coding=utf-8 说明。
因为怕后面出现问题,所以代码中的每个输出的地方,我还是 print(u"...") 表示了。

剩下疑问:
appium-server 的设置中有什么要设置中文的地方吗?
安卓手机端有什么需要设置中文的地方吗?

这个问题最终解决了吗?我碰到了一样的问题。

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册