0
0
Selenium Javatesting~20 mins

Getting text and attributes in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Attribute & Text 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 getText() vs getAttribute("value")?
Consider the following HTML input element:
<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);
A
Text: Hello
Value: Hello
B
Text: 
Value: 
C
Text: Hello
Value: 
D
Text: 
Value: Hello
Attempts:
2 left
💡 Hint
Remember that getText() returns visible text between tags, while getAttribute() fetches attribute values.
assertion
intermediate
1:30remaining
Which assertion correctly verifies the placeholder attribute?
Given a WebElement 'searchBox' representing:
<input type="search" placeholder="Search here">

Which assertion correctly checks that the placeholder is exactly "Search here"?
AassertEquals("Search here", searchBox.getAttribute("placeholder"));
BassertEquals("Search here", searchBox.getText());
CassertTrue(searchBox.getText().contains("Search here"));
DassertTrue(searchBox.getAttribute("value").equals("Search here"));
Attempts:
2 left
💡 Hint
Placeholder is an attribute, not visible text.
🔧 Debug
advanced
2:00remaining
Why does getText() return empty on a with nested elements?
HTML snippet:
<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);
AgetText() does not retrieve text from elements with nested tags.
BThe <span> element is hidden by CSS, so getText() returns empty.
CThe WebElement was not found, so getText() returns empty.
DThe page has not fully loaded, so getText() returns empty.
Attempts:
2 left
💡 Hint
Check if the element or its children are visible.
framework
advanced
2: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?
AUse WebDriverWait with ExpectedConditions.attributeToBe(button, "disabled", "false").
BUse a loop with getAttribute("disabled") and break when value is "false".
CUse JavascriptExecutor to poll the attribute value every second.
DUse Thread.sleep(5000) to wait 5 seconds before checking attribute.
Attempts:
2 left
💡 Hint
Selenium provides explicit waits for attribute conditions.
🧠 Conceptual
expert
2: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"?
AgetAttribute("class") returns visible text; getText() returns attribute values.
BgetText() returns the attribute value if the element has no child elements.
CgetAttribute("class") returns the HTML attribute value; getText() returns visible text inside the element.
DBoth methods return the same value but in different formats.
Attempts:
2 left
💡 Hint
Think about what attributes and text represent in HTML.