0
0
Selenium Pythontesting~10 mins

Modifying element attributes with JS in Selenium Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to change the 'value' attribute of an input element using JavaScript.

Selenium Python
driver.execute_script("arguments[0].setAttribute('value', [1])", element)
Drag options to blanks, or click blank then click option'
A"new text"
Bnew text
C'new text'
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the new value string.
Passing the value without quotes causing JavaScript errors.
2fill in blank
medium

Complete the code to remove the 'disabled' attribute from a button element using JavaScript.

Selenium Python
driver.execute_script("arguments[0].[1]('disabled')", button)
Drag options to blanks, or click blank then click option'
AgetAttribute
BsetAttribute
CremoveAttribute
DhasAttribute
Attempts:
3 left
💡 Hint
Common Mistakes
Using setAttribute instead of removeAttribute.
Trying to get or check the attribute instead of removing it.
3fill in blank
hard

Fix the error in the code to correctly change the 'class' attribute of an element using JavaScript.

Selenium Python
driver.execute_script("arguments[0].setAttribute('class', [1])", elem)
Drag options to blanks, or click blank then click option'
A"new-class"
Bnew_class
C'new-class'
Dnew-class
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the class name without quotes causing JavaScript syntax errors.
Using underscores instead of hyphens in class names.
4fill in blank
hard

Fill both blanks to set the 'title' attribute of an element to 'Hello World' using JavaScript.

Selenium Python
driver.execute_script("arguments[0].[1]([2], 'Hello World')", elem)
Drag options to blanks, or click blank then click option'
AsetAttribute
BgetAttribute
C'title'
D'value'
Attempts:
3 left
💡 Hint
Common Mistakes
Using getAttribute instead of setAttribute.
Passing the attribute name without quotes.
5fill in blank
hard

Fill all three blanks to toggle the 'hidden' attribute on an element using JavaScript.

Selenium Python
driver.execute_script("if (arguments[0].hasAttribute([1])) { arguments[0].[2]([3]); } else { arguments[0].setAttribute([1], '') }", elem)
Drag options to blanks, or click blank then click option'
A'hidden'
BremoveAttribute
DsetAttribute
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'hidden' without quotes causing syntax errors.
Confusing setAttribute and removeAttribute methods.