Challenge - 5 Problems
Attribute Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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'))
Attempts:
2 left
💡 Hint
The get_attribute method returns the value of the attribute if it exists, otherwise None.
✗ Incorrect
The DummyElement's get_attribute method returns 'https://example.com' when asked for 'href'. So the print statement outputs that string.
❓ assertion
intermediate1: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')Attempts:
2 left
💡 Hint
Use '==' to compare strings in Python, not 'is' or '='.
✗ Incorrect
Option A uses the correct equality operator '==' to compare strings. Option A uses 'is' which checks identity, not equality. Option A uses a method that doesn't exist for strings. Option A uses assignment '=' inside assert which is invalid syntax.
❓ locator
advanced2: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?
Attempts:
2 left
💡 Hint
Use CSS selectors for custom attributes with quotes.
✗ Incorrect
Option C correctly uses CSS selector with attribute selector and quotes. Option C looks for an ID which is different. Option C now correctly includes quotes around the attribute value to avoid XPath error. Option C looks for a class name which is unrelated.
🔧 Debug
advanced1: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()Attempts:
2 left
💡 Hint
Check the method signature for get_attribute.
✗ Incorrect
get_attribute requires one argument: the attribute name. Calling it without arguments causes a TypeError.
❓ framework
expert2: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
Attempts:
2 left
💡 Hint
Use '==' for string comparison in assertions.
✗ Incorrect
Option B correctly asserts string equality. Option B uses 'is' which checks identity, not equality. Option B uses a non-existent method. Option B uses assignment inside assert which is invalid.