AttributeError: ‘str‘ object has no attribute ‘decode‘ Python3
AttributeError: ‘str’ object has no attribute ‘decode’ Python3
虽是新进,但也要靠自己解决问题
起因:
我正在自学python + selenium ui自动化测试,目前正在学第6章的 数据驱动,已经学到
6.3 Csv实战了。
但学到这里我遇到了一个问题,问题如下:
1.我按照 书本内容把代码打了一遍(在pycharm),如下:
`import csv
import unittest
from selenium import webdriver
def readCsv(row,col):
rows = []
with open(‘test.csv’) as f:
reader = csv.reader(f)
next(reader,None)
for iter in reader:
rows.append(iter)
return ‘’.join(rows[row][col]).decode(‘gb2312’)
class SinaLogin(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.maximize_window()
self.driver.get(‘http://mail.sina.com.cn/’)
self.driver.implicitly_wait(30)
def tearDown(self):
self.driver.quit()
def login(self,username,password):
self.driver.find_element_by_id('freename').send_keys(username)
self.driver.find_element_by_id('freepassword').send_keys(password)
self.driver.find_element_by_link_text(u'登录').click()
def divText(self):
divText = self.driver.find_element_by_xpath('/html/body/div[1]/div/div[2]'
'/div/div/div[4]/div[1]/div[1]'
'/div[1]/span[1]')
return divText.text
def test_username_password_null(self):
'''验证:测试用户名和密码都为空的错误提示信息'''
self.login(readCsv(0,0),readCsv(0,1))
self.assertEqual(self.divText(),readCsv(0,2))
def test_sina_password_null(self):
'''验证:测试用户名为空密码不为空的错误提示'''
self.login(readCsv(1,0),readCsv(1,1))
self.assertTrue(self.divText(),readCsv(1,2))
def test_sina_username_format(self):
'''验证:测试用户名邮箱格式不正确的错误提示信息'''
self.login(readCsv(2,0),readCsv(2,1))
self.assertEqual(self.divText(),readCsv(2,2))
if name == ‘main’:
unittest.main(verbosity = 2)
`
2.运行后报错,如下:
2.1 然后我他妈的就开始琢磨咋回事–搜索报错信息(python3’str’ object has no attribute ‘decode’)找了半天解决方法,最后明确如下:
是这个狂出部分的问题
解决方法是:改为 encode就行了
但是,改了之后运行还是报错,如下:
首先是这个
然后是这个
我没管第一个,只针对第二个错误进行修改,搜索报错信息(python3 encode)找到了这里:https://www.runoob.com/python3/python3-string-encode.html
然后我就按照这里的教程又蒙了一次代码,如下:
然后就能正常运行了…如下:
目前还有个疑问我搞不清楚:为啥我的用例是从第二 到 第三 再到 第一 这个样顺序运行的?
推荐阅读
-
Django 报错:‘AttributeError: ‘str‘ object has no attribute ‘decode‘‘
-
AttributeError: ‘NoneType‘ object has no attribute ‘name‘
-
【已解决】python-pip升级报错- AttributeError: 'NoneType' object has no attribute 'bytes'
-
AttributeError: ‘str‘ object has no attribute ‘decode‘ Python3
-
Django2.2报错::AttributeError: ''str'' object has no attribute ''decode''
-
Python3下错误AttributeError: ‘dict’ object has no attribute’iteritems‘的分析与解决
-
Python3下错误AttributeError: ‘dict’ object has no attribute’iteritems‘的分析与解决
-
AttributeError: 'dict' object has no attribute 'has_key'
-
python错误:AttributeError: 'module' object has no attribute 's
-
python编程排除163邮箱发送邮件报错(AttributeError: ‘tuple‘ object has no attribute ‘encode‘)