我在Python的类继承中遇到了问题;也许与继承无关,但我没有其他想法。我正在使用selenium web-driver。这是我使用的代码:
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
类中;但是同样引发了异常。你对此有什么想法吗?
编辑 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'
returnedList = UIInterface.parentMethod(itemClassName = 'SomeClassName')
。 - ajknzhol