2014-09-28 63 views
2

夥計們是關於Python + Selenium auto的。試驗。AttributeError:'WebDriver'對象沒有屬性'id'

當我嘗試使用它的ID檢查打開的頁面上是否有元素時,出現「AttributeError」的問題。

它爲我的情況下,我籤標題,下面是我的代碼的一部分:

driver = webdriver.Firefox() 
    driver.get("https://accounts.google.com/ServiceLogin?sacu=1&scc=1&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&hl=ru&service=mail") 
    wait = WebDriverWait(driver, 10) 
    element = wait.until(EC.element_to_be_clickable((By.ID,"Email")))  
    self.assertIn("Gmail", driver.title) - works fine 
    self.assertIn("Email", driver.id) - AttributeError occurred 

任何人都可以有此問題的幫助?

+0

請發佈整個錯誤信息。 – bmargulies 2014-09-28 18:16:51

回答

3

driverwebdriver.Firefox的一個實例,也代表了它當前正在訪問的網頁。這就是爲什麼你能夠訪問title屬性。

但它看起來像你想要的是頁面上的元素id。如果是這樣,你需要首先找到它。也許你在找這個?

email_input = driver.find_element_by_id("Email") 
self.assertIn("Email", email_input.get_attribute('id')) 

如果WebDriver找不到元素,則會引發NoSuchElementException。所以,如果你只是想驗證是否有與它的ID「電子郵件」頁面上的一個元素,那麼你可以做一個查找和斷言,它並沒有錯誤,像這樣:

try: 
    email_input = driver.find_element_by_id("Email") 
except NoSuchElementException: 
    self.fail("Could not find 'Email' element on page")