Python中的类:AttributeError

时间:2016-12-16 00:44:02

标签: python-3.x class selenium inheritance

我正在使用Python中的Selenium WebDriver编写用于测试自动化的简单脚本,但问题与Python有关,而不是与Selenium有关。 有两个类FindByXPATH_1(base)& FindByXPATH_2(派生)。我想调用一个属性" driver"来自FindByXPATH_2方法的基类,但是当我运行代码时,AttributeError显示出来:"输入对象' FindByXPATH_1'没有属性' driver'"

以下是代码:

class FindByXPATH_1():
    def __init__(self):
        self.driver_location = '/usr/local/bin/chromedriver'
        self.driver = webdriver.Chrome(self.driver_location)
        self.driver.get('https://letskodeit.teachable.com/p/practice')

from basics.xpath_1 import FindByXPATH_1
import basics #the classes are in two different python files

class FindByXpath_2(FindByXPATH_1):
    def __init__(self):
        FindByXPATH_1.__init__(self)

    def find_by_starts_with(self):
        starting_with = FindByXPATH_1.driver.find_elements(By. XPATH, 
        '//div[@class="view-school"]//h3[starts-with(@)class, "subtitle"]')
        print(len(starting_with))

test = FindByXPATH_2()
test.find_by_starts_with()

运行代码后,我收到一条消息" AttributeError:type object' FindByXPATH_1'没有属性' driver'" 我该如何调用该属性?

1 个答案:

答案 0 :(得分:1)

在这一行:

    starting_with = FindByXPATH_1.driver.find_elements(By. XPATH, 
    '//div[@class="view-school"]//h3[starts-with(@)class, "subtitle"]')

你应该调用self.driver.find_elements,否则你试图访问FindByXPATH_1的类变量而不是实例变量driver