UnicodeWarning

来源:互联网 发布:mips linux gnu gcc 编辑:程序博客网 时间:2024/06/07 01:12

环境:

使用python2.7

错误示范:

#encoding:UTF-8

from selenium import webdriver

import unittest

class Login(unittest.TestCase):

    def setUp(self):

        self.driver=webdriver.Chrome(executable_path='/Users/xxx/Downloads/chromedriver')

        self.driver.implicitly_wait(3)

        self.base_url = "https://www.baidu.com"

    def test_jump_login(self):

        driver=self.driver

        driver.get(self.base_url + "/")

        act_title=driver.title

        expect_title="百度一下,你就知道"

        self.assertTrue(act_title==expect_title,msg="jump  incorrect")

def tearDown(self):

        driver=self.driver

        driver.quit()

if __name__=="__main__":

    unittest.main()

错误提示:

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

  self.assertTrue(act_title==expect_title,msg="jump  incorrect")

解决方案1:

expect_title="百度一下,你就知道"

改成:!!

expect_title=u"百度一下,你就知道"


解决方案2:

act_title=driver.title

改成:!!

act_title=driver.title.encode('utf-8')


python2的编码和解码是需要学习的基础






原创粉丝点击