2016-12-16 67 views
0

我正在使用Python中的Selenium WebDriver编写简单的测试自动化脚本,但问题与Python相关,而与Selenium无关。 有两个类FindByXPATH_1(base)& FindByXPATH_2(派生)。我想用FindByXPATH_2的方法在基类中调用属性“driver”,但是当我运行代码时,出现AttributeError:“type object'FindByXPATH_1''没有属性'driver'”Python中的类:AttributeError

这是代码:“AttributeError错误:类型的对象‘FindByXPATH_1’有没有属性‘司机’”

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

运行代码我得到一个消息后 我怎么能称之为属性?

回答

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

相关问题