Python属性错误:对象没有属性“self”

2024-09-29 23:20:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python中的类继承有一个问题;可能与继承无关,但我没有其他想法。我正在使用selenium web驱动程序。下面是我使用的代码:

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')行的错误;错误描述如下:

^{pr2}$

我认为这可能与继承有关,所以我尝试将parentMethod和{}放在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'

Tags: inpyselfdeflinefilewin32eclipse
1条回答
网友
1楼 · 发布于 2024-09-29 23:20:41

我在python2.7下用pip安装了selenium,并将代码改为使用Chrome驱动程序[1],并更改了检查器,以确保可以找到一些输入标记。完整代码如下。我在MacOSX上运行代码。它运行得很好。在

所以我想这里的问题不是代码。(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()

输出如下:

^{pr2}$

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

相关问题 更多 >

    热门问题