UnicodeWarning

本文介绍了一种在Python2中使用Selenium进行自动化测试时遇到的Unicode警告问题及其两种解决方案,涉及编码设置调整。

环境:

使用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的编码和解码是需要学习的基础






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值