Bird
Raised Fist0
Selenium Pythontesting~20 mins

Getting element attributes in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Attribute Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium Python code snippet?
Consider the following Selenium code that fetches an attribute from a web element. What will be printed?
Selenium Python
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.webdriver import WebDriver

class DummyDriver:
    def find_element(self, by, value):
        class DummyElement:
            def get_attribute(self, attr):
                if attr == 'href':
                    return 'https://example.com'
                return None
        return DummyElement()

driver = DummyDriver()
element = driver.find_element(By.ID, 'link')
print(element.get_attribute('href'))
Ahttps://example.com
BNone
CAttributeError
D'' (empty string)
Attempts:
2 left
💡 Hint
The get_attribute method returns the value of the attribute if it exists, otherwise None.
❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies the 'title' attribute of a web element?
You want to check that a web element's 'title' attribute equals 'Submit Button'. Which assertion is correct?
Selenium Python
title_value = element.get_attribute('title')
Aassert title_value == 'Submit Button'
Bassert element.get_attribute('title') is 'Submit Button'
Cassert title_value.equals('Submit Button')
Dassert title_value = 'Submit Button'
Attempts:
2 left
💡 Hint
Use '==' to compare strings in Python, not 'is' or '='.
❓ locator
advanced
2:00remaining
Which locator strategy is best to get an element by its 'data-test-id' attribute?
You want to find an element with attribute data-test-id='login-btn'. Which locator is correct and follows best practices?
Adriver.find_element(By.XPATH, '//*[@data-test-id="login-btn"]')
Bdriver.find_element(By.ID, 'login-btn')
Cdriver.find_element(By.CSS_SELECTOR, '[data-test-id="login-btn"]')
Ddriver.find_element(By.CLASS_NAME, 'login-btn')
Attempts:
2 left
💡 Hint
Use CSS selectors for custom attributes with quotes.
🔧 Debug
advanced
1:30remaining
What error does this Selenium code raise?
What error will this code raise when trying to get an attribute?
Selenium Python
element = driver.find_element(By.ID, 'submit')
value = element.get_attribute()
ANone
BAttributeError: 'WebElement' object has no attribute 'get_attribute'
CNoSuchElementException
DTypeError: get_attribute() missing 1 required positional argument: 'name'
Attempts:
2 left
💡 Hint
Check the method signature for get_attribute.
❓ framework
expert
2:30remaining
In a pytest Selenium test, which fixture usage correctly retrieves an element's attribute for assertion?
Given a pytest fixture 'driver' that provides a Selenium WebDriver instance, which test function correctly asserts the 'alt' attribute of an image element?
Selenium Python
import pytest
from selenium.webdriver.common.by import By

@pytest.fixture
def driver():
    # returns a WebDriver instance
    pass

def test_image_alt_attribute(driver):
    element = driver.find_element(By.TAG_NAME, 'img')
    alt_text = element.get_attribute('alt')
    # assertion here
Aassert alt_text = 'Company Logo'
Bassert alt_text == 'Company Logo'
Cassert alt_text.equals('Company Logo')
Dassert element.get_attribute('alt') is 'Company Logo'
Attempts:
2 left
💡 Hint
Use '==' for string comparison in assertions.

Practice

(1/5)
1. What does the Selenium method get_attribute('href') do when called on a web element?
easy
A. It sets the 'href' attribute to a new value.
B. It clicks the element if it has an 'href' attribute.
C. It returns the value of the 'href' attribute of the element.
D. It deletes the 'href' attribute from the element.

Solution

  1. Step 1: Understand the purpose of get_attribute

    The method get_attribute is used to read the value of a specified attribute from a web element.
  2. Step 2: Apply to 'href' attribute

    When called with 'href', it returns the URL or value assigned to the 'href' attribute of that element.
  3. Final Answer:

    It returns the value of the 'href' attribute of the element. -> Option C
  4. Quick Check:

    get_attribute('href') = attribute value [OK]
Hint: Remember: get_attribute reads, does not change attributes [OK]
Common Mistakes:
  • Thinking get_attribute changes or clicks elements
  • Confusing get_attribute with set_attribute
  • Expecting get_attribute to remove attributes
2. Which of the following is the correct syntax to get the 'class' attribute of a web element stored in variable element using Selenium in Python?
easy
A. element.getAttribute('class')
B. element.get_attribute('class')
C. element.getattribute('class')
D. element.getattr('class')

Solution

  1. Step 1: Recall correct method name in Selenium Python

    The correct method to get an attribute is get_attribute with an underscore, all lowercase.
  2. Step 2: Check method spelling and case

    element.get_attribute('class') uses get_attribute('class'), which matches the correct syntax exactly.
  3. Final Answer:

    element.get_attribute('class') -> Option B
  4. Quick Check:

    Correct method name = get_attribute [OK]
Hint: Use underscore: get_attribute, not camelCase [OK]
Common Mistakes:
  • Using camelCase like getAttribute
  • Misspelling method names
  • Using non-existent methods like getattr
3. Given the following code snippet:
element = driver.find_element(By.ID, 'submit-btn')
value = element.get_attribute('type')
print(value)

If the HTML is <button id='submit-btn' type='submit'>Send</button>, what will be printed?
medium
A. submit
B. None
C. Send
D. button

Solution

  1. Step 1: Identify the element and attribute

    The element is a button with id 'submit-btn' and attribute type='submit'.
  2. Step 2: Understand get_attribute('type') output

    Calling get_attribute('type') returns the string 'submit' as that is the attribute's value.
  3. Final Answer:

    submit -> Option A
  4. Quick Check:

    get_attribute('type') = 'submit' [OK]
Hint: get_attribute returns attribute value exactly as in HTML [OK]
Common Mistakes:
  • Confusing inner text with attribute value
  • Expecting tag name instead of attribute
  • Assuming None if attribute exists
4. You wrote this code:
element = driver.find_element(By.NAME, 'email')
email_value = element.get_attribute('value')
print(email_value)

But the print shows None even though the input field has a value. What is the most likely cause?
medium
A. The input field's value is set by JavaScript and not as an attribute.
B. The element was not found and element is None.
C. The attribute name 'value' is misspelled in get_attribute.
D. get_attribute cannot read 'value' attributes.

Solution

  1. Step 1: Understand difference between attribute and property

    Sometimes input values are set dynamically by JavaScript and not reflected as HTML attributes.
  2. Step 2: Recognize get_attribute reads HTML attributes only

    get_attribute('value') reads the attribute, but if value is set dynamically, it may return None.
  3. Final Answer:

    The input field's value is set by JavaScript and not as an attribute. -> Option A
  4. Quick Check:

    Dynamic values may not appear as attributes [OK]
Hint: Dynamic input values may not be in attributes [OK]
Common Mistakes:
  • Assuming get_attribute reads live input value always
  • Not checking if element was found
  • Misspelling attribute names
5. You want to verify that a checkbox is checked by reading its 'checked' attribute using Selenium in Python. The checkbox HTML is:
<input type='checkbox' id='agree' checked>

Which code snippet correctly checks if the checkbox is checked?
hard
A. is_checked = element.get_attribute('checked') == True
B. is_checked = element.get_attribute('checked') == 'false'
C. is_checked = element.get_attribute('checked') == 'checked'
D. is_checked = element.get_attribute('checked') is not None

Solution

  1. Step 1: Understand how 'checked' attribute works in HTML

    The 'checked' attribute is present without a value when the checkbox is checked.
  2. Step 2: Check attribute presence with get_attribute

    In Selenium, get_attribute('checked') returns 'true' if the checkbox is checked, None if unchecked.
  3. Step 3: Use presence check for 'checked'

    Since the attribute is present without value, checking if get_attribute('checked') is not None confirms it is checked.
  4. Final Answer:

    is_checked = element.get_attribute('checked') is not None -> Option D
  5. Quick Check:

    Presence of 'checked' attribute means checked [OK]
Hint: Check if get_attribute('checked') is not None for checked boxes [OK]
Common Mistakes:
  • Comparing to 'false' or 'checked' strings incorrectly
  • Using boolean True instead of string or None
  • Ignoring attribute presence vs value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard