0
0
Selenium Pythontesting~20 mins

Modifying element attributes with JS in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JS Attribute Modifier Master
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 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)
Aold_value
Bnew_value
CNone
DSyntaxError
Attempts:
2 left
💡 Hint
Think about what the JavaScript setAttribute method does to the element's attribute.
assertion
intermediate
1: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?
Aassert element.get_attribute('class') == 'active'
Bassert element.get_attribute('class') != 'active'
Cassert element.get_attribute('class') is None
Dassert element.get_attribute('class') == ''
Attempts:
2 left
💡 Hint
The attribute was set to 'active', so the assertion should confirm this exact value.
locator
advanced
1: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?
Adriver.find_element(By.CSS_SELECTOR, '[data-status="enabled"]')
Bdriver.find_element(By.ID, 'data-status')
Cdriver.find_element(By.CLASS_NAME, 'enabled')
Ddriver.find_element(By.NAME, 'data-status')
Attempts:
2 left
💡 Hint
Attributes can be targeted using CSS selectors with square brackets.
🔧 Debug
advanced
2: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'))
AThe element ID 'header' does not exist, so code fails silently
Bget_attribute('title') always returns None
Cexecute_script cannot modify element attributes
DSetting property 'title' does not update the attribute; setAttribute must be used
Attempts:
2 left
💡 Hint
Difference between DOM properties and HTML attributes matters here.
framework
expert
2: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?
AModify attribute with execute_script, then immediately assert attribute value without wait
BModify attribute by sending keys to element, then assert attribute value
CModify attribute with execute_script, then use WebDriverWait to wait until attribute value updates before asserting
DModify attribute by refreshing the page, then assert attribute value
Attempts:
2 left
💡 Hint
Consider timing and synchronization in Selenium tests.