Challenge - 5 Problems
Attribute Mastery
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 Java code snippet?
Consider the following Selenium Java code that tries to get the value of an input field's attribute. What will be printed?
Selenium Java
WebElement input = driver.findElement(By.id("username")); String value = input.getAttribute("value"); System.out.println(value);
Attempts:
2 left
💡 Hint
Remember that getAttribute returns the attribute value as in the HTML, not necessarily the visible text.
✗ Incorrect
The getAttribute("value") method returns the value attribute of the input element as defined in the HTML source or last set by JavaScript. It does not return the visible text typed by the user unless it matches the attribute value.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies that a button's disabled attribute is set to true?
You want to check if a button is disabled by verifying its "disabled" attribute using Selenium Java and JUnit. Which assertion is correct?
Selenium Java
WebElement button = driver.findElement(By.id("submitBtn")); String disabledAttr = button.getAttribute("disabled");
Attempts:
2 left
💡 Hint
The disabled attribute may be present without a value or with value "true".
✗ Incorrect
The disabled attribute in HTML is a boolean attribute. If present, getAttribute returns "true" or an empty string depending on the browser. Checking that it is not null confirms the attribute is present, meaning the button is disabled.
🔧 Debug
advanced2:00remaining
Why does this code fail to set the placeholder attribute?
This Selenium Java code tries to set the placeholder attribute of an input field but it does not work. What is the reason?
Selenium Java
WebElement input = driver.findElement(By.id("searchBox")); input.setAttribute("placeholder", "Search here");
Attempts:
2 left
💡 Hint
Check the WebElement API for attribute setting methods.
✗ Incorrect
Selenium WebElement does not provide a setAttribute method. To change attributes, JavaScript execution is required via JavascriptExecutor.
❓ framework
advanced2:00remaining
Which Selenium Java code correctly sets an attribute using JavaScript?
You want to set the "data-test" attribute of a WebElement using Selenium Java. Which code snippet correctly does this?
Selenium Java
WebElement element = driver.findElement(By.cssSelector(".test-element"));Attempts:
2 left
💡 Hint
Remember how to run JavaScript in Selenium Java.
✗ Incorrect
Only option B uses JavascriptExecutor correctly to run JavaScript on the element to set the attribute. WebElement does not have setAttribute method, and driver does not have executeScript without casting.
🧠 Conceptual
expert2:00remaining
What is the difference between getAttribute("value") and getText() for an input element?
In Selenium Java, what is the key difference between calling getAttribute("value") and getText() on an input element?
Attempts:
2 left
💡 Hint
Think about what getText() returns for input elements.
✗ Incorrect
Input elements do not have inner text, so getText() returns empty string. The current text typed in the input is stored in the "value" attribute, so getAttribute("value") returns that content.