python 出现UnicodeEncodeError: 'gbk' codec can't encode 解决方案

上一篇 / 下一篇  2020-06-23 16:39:53 / 个人分类:python



出现报错:
2020-06-23 15:32:26,749-basepage.py-get_ele_text-185-root-INFO-在提交订单页面—小计获取元素('xpath', '//wx-view[@id="total"]//wx-text[2]//span[2]')的文本
--- Logging error ---
Traceback (most recent call last):
  File "C:\Python36\lib\logging\__init__.py", line 995, in emit
    stream.write(msg)
UnicodeEncodeError: 'gbk' codec can't encode character '\xa5' in position 125: illegal multibyte sequence
Call stack:
  File "main.py", line 10, in <module>
    pytest.main(["-s", "-v", 'TestCases/test_order_h.py',"--html=Outputs/reports/report.html", "--reruns", "5", "--reruns-delay", "5", "--alluredir=Outputs/allure_reports"])
  File "C:\Python36\lib\site-packages\_pytest\config\__init__.py", line 125, in main
    config=config
  File "C:\Python36\lib\site-packages\pluggy\hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "C:\Python36\lib\site-packages\pluggy\manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "C:\Python36\lib\site-packages\pluggy\manager.py", line 87, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,



解决方案:在出现问题的前面直接encode("utf-8")

value = value.encode('utf-8')


完整方法:
defget_ele_text(self,loc,img_name,timeout=20,poll_fre=0.5):
self.wait_ele_visible(loc,img_name,timeout,poll_fre)
ele =self.get_element(loc,img_name)
self.logger.info("在{}获取元素{}的文本".format(img_name,loc))
try:
value = ele.text
exceptExceptionase:
self.save_page_shot(img_name)
self.logger.exception("获取元素文本失败!{}".format(e))
raise
else:
value = value.encode('utf-8')
self.logger.info("获取元素{}文本{}成功".format(loc,value))
returnvalue

TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-13  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 81336
  • 日志数: 94
  • 文件数: 1
  • 建立时间: 2017-04-14
  • 更新时间: 2020-11-17

RSS订阅

Open Toolbar