2014-05-20 75 views
4

我在Python中的類繼承有問題;也許它與繼承無關,但我沒有其他想法。我正在使用硒驅動程序。下面的代碼我使用:Python屬性錯誤:對象沒有屬性'self'

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;同樣的異常提出。對此有任何想法?

EDIT 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' 
+2

請後完整的錯誤消息。 – BrenBarn

+0

如何爲「InterfaceChild」類創建對象? –

+0

嘗試'returnedList = UIInterface.parentMethod(itemClassName ='SomeClassName')' – ajkumar25

回答

0

我與PIP的Python下安裝硒2.7並將代碼更改爲使用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/

相關問題