Challenge - 5 Problems
Attribute & Text Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of getText() vs getAttribute("value")?
Consider the following HTML input element:
And the Selenium Java code:
What will be printed?
<input type="text" value="Hello">
And the Selenium Java code:
WebElement input = driver.findElement(By.tagName("input"));
String text = input.getText();
String value = input.getAttribute("value");
System.out.println("Text: " + text);
System.out.println("Value: " + value);What will be printed?
Selenium Java
WebElement input = driver.findElement(By.tagName("input")); String text = input.getText(); String value = input.getAttribute("value"); System.out.println("Text: " + text); System.out.println("Value: " + value);
Attempts:
2 left
💡 Hint
Remember that getText() returns visible text between tags, while getAttribute() fetches attribute values.
✗ Incorrect
For input elements, getText() returns empty because input tags do not have inner text. The visible text is in the 'value' attribute, so getAttribute("value") returns "Hello".
❓ assertion
intermediate1:30remaining
Which assertion correctly verifies the placeholder attribute?
Given a WebElement 'searchBox' representing:
Which assertion correctly checks that the placeholder is exactly "Search here"?
<input type="search" placeholder="Search here">
Which assertion correctly checks that the placeholder is exactly "Search here"?
Attempts:
2 left
💡 Hint
Placeholder is an attribute, not visible text.
✗ Incorrect
The placeholder is an attribute of the input element, so getAttribute("placeholder") returns its value. getText() returns visible text, which is empty for input elements.
🔧 Debug
advanced2:00remaining
Why does getText() return empty on a with nested elements?
HTML snippet:
Selenium code:
The output is empty. What is the most likely reason?
<span id="label">Name: <strong>John</strong></span>
Selenium code:
WebElement label = driver.findElement(By.id("label"));
String text = label.getText();
System.out.println(text);The output is empty. What is the most likely reason?
Selenium Java
WebElement label = driver.findElement(By.id("label"));
String text = label.getText();
System.out.println(text);Attempts:
2 left
💡 Hint
Check if the element or its children are visible.
✗ Incorrect
If the or its children are hidden (e.g., display:none), getText() returns empty because Selenium only returns visible text.
❓ framework
advanced2:00remaining
Best practice to wait for attribute value change in Selenium Java?
You want to wait until a button's 'disabled' attribute becomes 'false'. Which Selenium Java approach is best?
Attempts:
2 left
💡 Hint
Selenium provides explicit waits for attribute conditions.
✗ Incorrect
WebDriverWait with ExpectedConditions.attributeToBe waits efficiently until the attribute matches the expected value, avoiding fixed delays or manual loops.
🧠 Conceptual
expert2:30remaining
Why might getAttribute("class") return different results than getText() for the same element?
Consider a
element with class="btn primary" and visible text "Submit".
Why does getAttribute("class") return "btn primary" but getText() returns "Submit"?
Why does getAttribute("class") return "btn primary" but getText() returns "Submit"?
Attempts:
2 left
💡 Hint
Think about what attributes and text represent in HTML.
✗ Incorrect
Attributes like 'class' are part of the element's HTML tag. getText() returns the text content visible to the user inside the element, which is different from attribute values.