Python属性错误:对象没有属性'self'

时间:2014-05-20 06:44:28

标签: python exception selenium self attributeerror

我在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>)

我认为它可能与继承有关,因此我尝试将parentMethodgetAllElements放入班级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'

1 个答案:

答案 0 :(得分:0)

我在Python 2.7下使用pip安装了selenium并更改了代码以使用Chrome驱动程序[1],并更改了检查程序以确保找到了一些输入标记。完整代码如下。我在Mac OS X上运行代码。它工作得很好。

所以我猜这里的问题不是代码。 (Windows不是Python的朋友,也许:)。

from selenium import webdriver

class UIInterface(object):
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('http://duckduckgo.com')

    def parentMethod(self, itemClassName = ''):
        allElements = self.getAllElements()
        result = [el for el in allElements]
        print('===', result)
        return result

    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')


if __name__ == '__main__':
    ieInterface = InterfaceChild()
    ieInterface.childMethod()

输出如下:

('===', [<selenium.webdriver.remote.webelement.WebElement object at 0x10e145cd0>, <selenium.webdriver.remote.webelement.WebElement object at 0x10e145d10>, <selenium.webdriver.remote.webelement.WebElement object at 0x10e145d50>])

[1] http://chromedriver.storage.googleapis.com/index.html?path=2.9/