运行python脚本时一直提示xx object has no attribute xx

代码如下
#coding=utf-8
import os
import time
import unittest

PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
)
class appTest(unittest.TestCase):
def setup(self):
desired_caps = {
'platformName':'Android',
'platformVersion':'4.2',

'deviceName':'Android',
'app':PATH(r'C:\Users\lc\Desktop\app\86785.apk'),
'appPackage':'com.example.android.contactmanager',
'appActivity':'.ContactManager'
}
self.driver = webdriver.Remote(r'xxxxxxx', desired_caps)

def tearDown(self):
self.driver.quit()

def test_addContact(self):
el=self.driver.find_element_by_name('Add Contact')
el.click()

textfileds=self.driver.find_elements_by_class_name('android.widget.EditText')
textfileds[0].send_keys("Appium User")
textfileds[1].send_keys("110")
textfileds[2].send_keys("[email protected]")

self.driver.find_element_by_name("Save").click()

if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(appTest)
unittest.TextTestRunner(verbosity=2).run(suite)

报错:
AttributeError: 'appTest' object has no attribute 'driver'

这其实是.pyc文件存在问题。

问题定位:

查看import库的源文件,发现源文件存在且没有错误,同时存在源文件的.pyc文件

问题解决方法:

1、命名py脚本时,不要与python预留字,模块名等相同。

2、删除该库的.pyc文件(因为py脚本每次运行时均会生成.pyc文件;在已经生成.pyc文件的情况下,若代码不更新,运行时依旧会走pyc,所以要删除.pyc文件),重新运行代码;或者找一个可以运行代码的环境,拷贝替换当前机器的.pyc文件即可。

扩展资料:

修改别人python代码,会遇到在类的init中已定义self,但后面使用还是找不到定义的self.*,其中一个原因是init中self定义顺序的问题。

Python是一种跨平台的计算机程序设计语言。是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的、大型项目的开发。

Python已经成为最受欢迎的程序设计语言之一。自从2004年以后,python的使用率呈线性增长。Python 2于2000年10月16日发布,稳定版本是Python 2.7。

参考资料:

百度百科-python

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-07-29

你没有导入webdriver的包

from appium import webdriver

追问

这个有写的、。估计没有复制上去,

第2个回答  推荐于2017-09-16
def setup(self):应该是def setUp(self):
注意大小写

然后还有个错误,webdriver没有定义,这个就不知道了本回答被提问者采纳
相似回答