Python Attribute Error: object has no attribute 'self'
Asked Answered
S

1

6

I have a problem in class inheritance in Python; maybe it's not related to inheritance, but I have no other idea. I'm working with selenium web-driver. Here's the code I use:

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

This code gives me an error for line returnedList = self.parentMethod(itemClassName = 'SomeClassName'); the error description is this:

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

I thought it might be related to inheritance, so I tried to put parentMethod and getAllElements in the class InterfaceChild; same exception raised. Any idea about this??

EDIT 1: This is the main method for making instance of classes:

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

This is the complete stack-trace:

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'
Selfexecuting answered 20/5, 2014 at 6:44 Comment(5)
Please post the complete error message.Scabious
how you are creating the object for "InterfaceChild" class?Coefficient
Try returnedList = UIInterface.parentMethod(itemClassName = 'SomeClassName')Samsun
I edited the question and has added the complete stack-trace and also the main method.Selfexecuting
@ajkumar25, it gives me the exactly same error.Selfexecuting
C
0

I installed selenium with pip under Python 2.7 and changed the code to use Chrome driver[1] instead and changed the checker to make sure there would be some input tags being found. full code below. I'm running the code on Mac OS X. it works just fine.

So I guess the issue here is not the code. (Windows is not a friend of Python, perhaps :).

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

The output looks like:

('===', [<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/

Consociate answered 20/5, 2014 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.