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.
✗ 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
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"?
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
advanced
2:00remaining
Why does getText() return empty on a with nested elements?
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.
✗ Incorrect
If the or its children are hidden (e.g., display:none), getText() returns empty because Selenium only returns visible text.
❓ 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.
✗ Incorrect
WebDriverWait with ExpectedConditions.attributeToBe waits efficiently until the attribute matches the expected value, avoiding fixed delays or manual loops.
🧠 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"?
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.
✗ 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.
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
Step 1: Understand the purpose of getText()
The getText() method returns the visible text inside a web element on the page.
Step 2: Differentiate from getAttribute()
getAttribute() retrieves attribute values like 'href' or 'class', not visible text.
Final Answer:
getText() -> Option B
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
Step 1: Recall getAttribute() usage
To get an attribute value, use getAttribute("attributeName") on the WebElement.
Step 2: Check syntax correctness
String url = link.getAttribute("href"); correctly calls getAttribute("href") and assigns it to a String variable.
Final Answer:
String url = link.getAttribute("href"); -> Option A
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
Step 1: Understand getText() output
getText() returns the visible text "Submit" from the button element.
Step 2: Understand getAttribute() output
getAttribute("type") returns the attribute value "button".
Step 3: Analyze print statement
Prints text + "," + type, which is "Submit,button".
Final Answer:
Submit,button -> Option D
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:
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
Step 1: Identify why getText() returns empty
getText() returns visible text inside element, but placeholder is an attribute, not visible text.
Step 2: Correct method to get attribute value
Use getAttribute("placeholder") to get the placeholder attribute's value.
Final Answer:
Use getAttribute("placeholder") instead of getText() -> Option A
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
Step 1: Verify visible text with getText()
Use assertEquals(link.getText(), "Home") to check visible text exactly.
Step 2: Verify href attribute contains substring
Use assertTrue(link.getAttribute("href").contains("index.html")) to check href contains the expected part.
Final Answer:
assertEquals(link.getText(), "Home"); assertTrue(link.getAttribute("href").contains("index.html")); -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.