2016-12-16 69 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

相關問題