aaz
aaz

Reputation: 43

AttributeError: 'WebDriver' object has no attribute 'id'

Guys it's about Python+Selenium auto. tests.

The problem that I'm getting "AttributeError" when trying to check if I have an element on opened page using it's ID.

It works for me in case I'm checkin the title, below is the part of my code:

    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

Can anyone help with this issue?

Upvotes: 4

Views: 21861

Answers (1)

nofinator
nofinator

Reputation: 3023

driver is an instance of webdriver.Firefox and also represents the web page it's currently accessing. That's why you're able to access the title attribute.

But it looks like what you want is the id of an element on the page. If so, you need to find that first. Perhaps you're looking for this?

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

If WebDriver can't find an element, it will raise NoSuchElementException. So if you only want to verify that there is an element on the page with "Email" in its id, then you can just do a find and assert that it did not error, like this:

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

Upvotes: 3

Related Questions