python错误:UnicodeDecodeError: 'utf8' codec can't decode byte 0xe6 in position 0: unexpected end of data

一、错误原因

在学习selenium自动化测试框架的时候,进行模仿浏览器搜索功能,输入英文是没问题,但是输入中文就报错,报错代码

    def test_baidu_search(self):
        """
        这里一定要test开头,把测试逻辑代码封装到一个test开头的方法里。
        :return:
        """
        self.driver.find_element_by_id('kw').send_keys('时间')
        time.sleep(1)
        try:
            assert '时间' in self.driver.title
            print ('Test Pass.')
        except Exception as e:
            print ('Test Fail.', format(e))

报错内容:UnicodeDecodeError: 'utf8' codec can't decode byte 0xe6 in position 0: unexpected end of data

二、解决办法

在中文后加.decode("utf-8") 设置为utf-8

 assert '时间'.decode("utf-8") in self.driver.title
原文地址:https://www.cnblogs.com/huguodong/p/7478697.html