Challenge - 5 Problems
JS Attribute Modifier Master
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 Python code that modifies an element's attribute using JavaScript. What will be the value of the attribute
data-test after execution?Selenium Python
from selenium import webdriver from selenium.webdriver.common.by import By class DummyElement: def __init__(self): self.attrs = {"data-test": "old_value"} def get_attribute(self, name): return self.attrs.get(name, None) def set_attribute(self, name, value): self.attrs[name] = value # Simulated element element = DummyElement() # JavaScript to modify attribute js_script = "arguments[0].setAttribute('data-test', 'new_value')" # Simulate JS execution exec(js_script.replace('arguments[0].setAttribute', 'element.set_attribute')) result = element.get_attribute('data-test') print(result)
Attempts:
2 left
💡 Hint
Think about what the JavaScript setAttribute method does to the element's attribute.
✗ Incorrect
The JavaScript code sets the 'data-test' attribute to 'new_value'. The simulated Python code replaces the JS call with a method that updates the attribute dictionary. Therefore, the attribute value after execution is 'new_value'.
❓ assertion
intermediate1:30remaining
Which assertion correctly verifies the attribute change after JS execution?
You have modified an element's attribute
class using JavaScript in Selenium Python. Which assertion correctly checks that the attribute was updated to active?Selenium Python
element = driver.find_element(By.ID, 'my-element') driver.execute_script("arguments[0].setAttribute('class', 'active')", element) # Which assertion below is correct?
Attempts:
2 left
💡 Hint
The attribute was set to 'active', so the assertion should confirm this exact value.
✗ Incorrect
After executing the JavaScript to set the 'class' attribute to 'active', the element's attribute value should be exactly 'active'. Therefore, the assertion checking equality to 'active' is correct.
❓ locator
advanced1:30remaining
Which locator is best to find an element by a modified attribute using Selenium?
After modifying an element's
data-status attribute to enabled using JavaScript, which Selenium locator will correctly find this element?Attempts:
2 left
💡 Hint
Attributes can be targeted using CSS selectors with square brackets.
✗ Incorrect
The CSS selector '[data-status="enabled"]' selects elements with the attribute 'data-status' equal to 'enabled'. Other locators do not target attributes directly or use incorrect methods.
🔧 Debug
advanced2:00remaining
Why does this Selenium Python code fail to modify the attribute?
Review the code below. Why does the attribute
title remain unchanged after execution?Selenium Python
element = driver.find_element(By.ID, 'header') driver.execute_script("arguments[0].title = 'New Title'", element) # Check attribute print(element.get_attribute('title'))
Attempts:
2 left
💡 Hint
Difference between DOM properties and HTML attributes matters here.
✗ Incorrect
Setting the property 'title' changes the DOM property but does not update the HTML attribute. Selenium's get_attribute reads the attribute, so it remains unchanged. Using setAttribute updates the attribute correctly.
❓ framework
expert2:30remaining
In a Selenium test framework, which approach ensures attribute modification is verified reliably?
You want to modify an element's attribute using JavaScript and verify the change in a Selenium Python test. Which approach below is best practice to ensure the test is reliable and stable?
Attempts:
2 left
💡 Hint
Consider timing and synchronization in Selenium tests.
✗ Incorrect
JavaScript execution may be asynchronous or the DOM may take time to update. Using WebDriverWait to wait for the attribute change ensures the test does not fail due to timing issues, making it reliable.