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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM