Bird
Raised Fist0
Selenium Javatesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which Selenium WebDriver method is used to retrieve the visible text from a web element in Java?
easy
A. getValue()
B. getText()
C. getAttribute()
D. getContent()

Solution

  1. Step 1: Understand the purpose of getText()

    The getText() method returns the visible text inside a web element on the page.
  2. Step 2: Differentiate from getAttribute()

    getAttribute() retrieves attribute values like 'href' or 'class', not visible text.
  3. Final Answer:

    getText() -> Option B
  4. Quick Check:

    Visible text = getText() [OK]
Hint: Use getText() for visible text, getAttribute() for attributes [OK]
Common Mistakes:
  • Confusing getText() with getAttribute()
  • Using getValue() which is not a Selenium method
  • Trying to get text with getContent() which doesn't exist
2. Which of the following is the correct syntax to get the value of the 'href' attribute from a WebElement named link in Selenium Java?
easy
A. String url = link.getAttribute("href");
B. String url = link.getText("href");
C. String url = link.getValue("href");
D. String url = link.getHref();

Solution

  1. Step 1: Recall getAttribute() usage

    To get an attribute value, use getAttribute("attributeName") on the WebElement.
  2. Step 2: Check syntax correctness

    String url = link.getAttribute("href"); correctly calls getAttribute("href") and assigns it to a String variable.
  3. Final Answer:

    String url = link.getAttribute("href"); -> Option A
  4. Quick Check:

    Attribute value = getAttribute("name") [OK]
Hint: Use getAttribute("attr") syntax to get attribute values [OK]
Common Mistakes:
  • Using getText() to get attribute values
  • Calling non-existent methods like getHref()
  • Passing attribute name as parameter to getText()
3. Consider the following Selenium Java code snippet:
WebElement button = driver.findElement(By.id("submitBtn"));
String text = button.getText();
String type = button.getAttribute("type");
System.out.println(text + "," + type);
If the button's visible text is "Submit" and its type attribute is "button", what will be printed?
medium
A. button
B. button,Submit
C. Submit
D. Submit,button

Solution

  1. Step 1: Understand getText() output

    getText() returns the visible text "Submit" from the button element.
  2. Step 2: Understand getAttribute() output

    getAttribute("type") returns the attribute value "button".
  3. Step 3: Analyze print statement

    Prints text + "," + type, which is "Submit,button".
  4. Final Answer:

    Submit,button -> Option D
  5. Quick Check:

    Text + "," + Attribute = Submit,button [OK]
Hint: getText() shows visible text; getAttribute() shows attribute value [OK]
Common Mistakes:
  • Mixing order of printed values
  • Expecting getText() to return attribute value
  • Ignoring the comma in print output
4. You wrote this code to get the placeholder attribute of an input field:
WebElement input = driver.findElement(By.name("email"));
String placeholder = input.getText();
But placeholder is empty. What is the error and how to fix it?
medium
A. Use getAttribute("placeholder") instead of getText()
B. Use getText() but call it after clicking the input
C. Use getValue() instead of getText()
D. Use getText() with parameter "placeholder"

Solution

  1. Step 1: Identify why getText() returns empty

    getText() returns visible text inside element, but placeholder is an attribute, not visible text.
  2. Step 2: Correct method to get attribute value

    Use getAttribute("placeholder") to get the placeholder attribute's value.
  3. Final Answer:

    Use getAttribute("placeholder") instead of getText() -> Option A
  4. Quick Check:

    Attributes need getAttribute() [OK]
Hint: Use getAttribute() for attributes like placeholder [OK]
Common Mistakes:
  • Using getText() to get attribute values
  • Trying to pass attribute name to getText()
  • Using non-existent getValue() method
5. You want to verify that a link's visible text is "Home" and its href attribute contains "index.html". Which Selenium Java code snippet correctly asserts both conditions?
hard
A. assertEquals(link.getText(), "index.html"); assertTrue(link.getAttribute("href").contains("Home"));
B. assertEquals(link.getAttribute("text"), "Home"); assertTrue(link.getText().contains("index.html"));
C. assertEquals(link.getText(), "Home"); assertTrue(link.getAttribute("href").contains("index.html"));
D. assertTrue(link.getText().equals("Home")); assertEquals(link.getAttribute("href"), "index.html");

Solution

  1. Step 1: Verify visible text with getText()

    Use assertEquals(link.getText(), "Home") to check visible text exactly.
  2. Step 2: Verify href attribute contains substring

    Use assertTrue(link.getAttribute("href").contains("index.html")) to check href contains the expected part.
  3. Final Answer:

    assertEquals(link.getText(), "Home"); assertTrue(link.getAttribute("href").contains("index.html")); -> Option C
  4. Quick Check:

    Text = getText(), Attribute = getAttribute() [OK]
Hint: Assert visible text with getText(), attribute with getAttribute() [OK]
Common Mistakes:
  • Using getAttribute("text") which is invalid
  • Comparing href exactly instead of contains
  • Mixing up text and href in assertions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard