4
我在Python中的類繼承有問題;也許它與繼承無關,但我沒有其他想法。我正在使用硒驅動程序。下面的代碼我使用:Python屬性錯誤:對象沒有屬性'self'
from selenium import webdriver
class UIInterface(object):
def __init__(self):
self.driver = webdriver.Ie()
self.driver.get('URL')
def parentMethod(self, itemClassName = ''):
allElements = self.getAllElements()
return [el for el in allElements if el.get_attribute('type') == 'checkbox']
def getAllElements(self):
return self.driver.find_elements_by_tag_name('input')
class InterfaceChild(UIInterface):
def __init__(self):
super(InterfaceChild, self).__init__()
def childMethod(self):
returnedList = self.parentMethod(itemClassName = 'SomeClassName')
for item in returnedList:
print item.get_attribute('innerHTML')
此代碼給我行returnedList = self.parentMethod(itemClassName = 'SomeClassName')
錯誤;錯誤描述是這樣的:
(<type 'exceptions.AttributeError'>, AttributeError("'InterfaceChild' object has no attribute 'self'",), <traceback object at 0x000000000418E5C8>)
我想這可能與遺傳有關,所以我試圖把parentMethod
和getAllElements
在類InterfaceChild
;同樣的異常提出。對此有任何想法?
EDIT 1: 這是爲了使類實例的主要方法:
if __name__ == '__main__':
ieInterface = InterfaceChild()
ieInterface.childMethod()
這是完整的堆棧跟蹤:
Traceback (most recent call last):
File "C:\Program Files\Eclipse\eclipse-jee-helios-win32\eclipse-jee-helios-win32\plugins\org.python.pydev_2.8.2.2013090511\pysrc\pydevd.py", line 1446, in <module>
debugger.run(setup['file'], None, None)
File "C:\Program Files\Eclipse\eclipse-jee-helios-win32\eclipse-jee-helios-win32\plugins\org.python.pydev_2.8.2.2013090511\pysrc\pydevd.py", line 1092, in run
pydev_imports.execfile(file, globals, locals) #execute the script
File "D:\workspace\testCode.py", line 133, in main
ieInterface.childMethod()
File "D:\workspace\testCode.py", line 33, in childMethod
returnedList = self.parentMethod(itemClassName = 'SomeClassName')
File "D:\workspace\testCode.py", line 45, in parentMethod
allElements = self.getAllElements()
AttributeError: 'InterfaceChild' object has no attribute 'self'
請後完整的錯誤消息。 – BrenBarn
如何爲「InterfaceChild」類創建對象? –
嘗試'returnedList = UIInterface.parentMethod(itemClassName ='SomeClassName')' – ajkumar25