selenium+python 中遇到的编解码问题

来源:互联网 发布:java实现ftp上传文件 编辑:程序博客网 时间:2024/05/16 08:00

运行环境python2.7

执行到此行代码:assert alert_test == "已经记录下您的使用偏好",'test error'

【报错】

UnicodeWarning: Unicode equalcomparison failed to convert both arguments to Unicode -interpreting them as being unequal

错误分析:

alert_test打印出来的内容为“已经记录下您的使用偏好”,但仍报下列错误,初步断定为编码不一致导致的错误

【解决方案】

可以简单翻一下警告:uncode编码警告:在unicode等价比较中,把两个参数同时转换为unicode编码失败。中断并认为他们不相等。

python里一般处理的是unicode和str的文本对象,另外,python程序本身的utf-8个是编码,str对象的文本转换为unicode需要使用text.decode("utf-8").

所以把上行代码修改为:

assert alert_test == ("已经记录下您的使用偏好").decode("utf-8"),'testerror'
原创粉丝点击