0
0
Selenium Pythontesting~20 mins

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

Choose your learning style9 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.