【发布时间】:2014-07-08 07:05:37
【问题描述】:
我在 Python 中的类继承有问题;也许它与继承无关,但我没有其他想法。我正在使用硒网络驱动程序。这是我使用的代码:
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'
【问题讨论】:
-
请发布完整的错误信息。
-
你是如何为“InterfaceChild”类创建对象的?
-
试试
returnedList = UIInterface.parentMethod(itemClassName = 'SomeClassName') -
我编辑了问题并添加了完整的堆栈跟踪以及主要方法。
-
@ajkumar25,它给了我完全相同的错误。
标签: python exception selenium self attributeerror