【问题标题】:Python Attribute Error: object has no attribute 'self'Python属性错误:对象没有属性'self'
【发布时间】: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>)

我认为可能与继承有关,所以我尝试将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'

【问题讨论】:

  • 请发布完整的错误信息。
  • 你是如何为“InterfaceChild”类创建对象的?
  • 试试returnedList = UIInterface.parentMethod(itemClassName = 'SomeClassName')
  • 我编辑了问题并添加了完整的堆栈跟踪以及主要方法。
  • @ajkumar25,它给了我完全相同的错误。

标签: python exception selenium self attributeerror


【解决方案1】:

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

【讨论】: