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

Zei*_*zar 5 python selenium exception 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')
Run Code Online (Sandbox Code Playgroud)

这段代码给我一个错误的行returnedList = self.parentMethod(itemClassName = 'SomeClassName'); 错误描述是这样的:

(<type 'exceptions.AttributeError'>, AttributeError("'InterfaceChild' object has no attribute 'self'",), <traceback object at 0x000000000418E5C8>)
Run Code Online (Sandbox Code Playgroud)

我想这可能与遗传有关,所以我试图把parentMethodgetAllElements在类InterfaceChild; 提出同样的例外.对此有什么想法?

编辑1: 这是制作类实例的主要方法:

if __name__ == '__main__':
    ieInterface = InterfaceChild()
    ieInterface.childMethod()
Run Code Online (Sandbox Code Playgroud)

这是完整的堆栈跟踪:

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'
Run Code Online (Sandbox Code Playgroud)

mit*_*tnk 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()
Run Code Online (Sandbox Code Playgroud)

输出看起来像:

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

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