Bird
Raised Fist0
Selenium Javatesting~10 mins

Getting text and attributes in Selenium Java - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the visible text of the web element.

Selenium Java
String text = element.[1]();
Drag options to blanks, or click blank then click option'
AgetAttribute
BgetText
CgetValue
DgetContent
Attempts:
3 left
💡 Hint
Common Mistakes
Using getAttribute instead of getText to get visible text.
Trying to use getValue which is not a WebElement method.
2fill in blank
medium

Complete the code to get the value of the 'href' attribute of the web element.

Selenium Java
String hrefValue = element.[1]("href");
Drag options to blanks, or click blank then click option'
AgetProperty
BgetValue
CgetText
DgetAttribute
Attempts:
3 left
💡 Hint
Common Mistakes
Using getText to get attribute values.
Using getValue which is not a WebElement method.
3fill in blank
hard

Fix the error in the code to correctly get the 'class' attribute of the element.

Selenium Java
String className = element.[1]("class");
Drag options to blanks, or click blank then click option'
AgetAttribute
BgetText
CgetClass
DgetProperty
Attempts:
3 left
💡 Hint
Common Mistakes
Using getClass which returns the Java class, not the attribute.
Using getText which returns visible text, not attribute.
4fill in blank
hard

Fill both blanks to get the text and the 'title' attribute of the element.

Selenium Java
String text = element.[1]();
String title = element.[2]("title");
Drag options to blanks, or click blank then click option'
AgetText
BgetValue
CgetAttribute
DgetContent
Attempts:
3 left
💡 Hint
Common Mistakes
Using getValue instead of getText for visible text.
Using getText to get attribute values.
5fill in blank
hard

Fill all three blanks to get the text, 'alt' attribute, and 'id' attribute of the element.

Selenium Java
String text = element.[1]();
String altText = element.[2]("alt");
String idValue = element.[3]("id");
Drag options to blanks, or click blank then click option'
AgetValue
BgetAttribute
CgetText
DgetProperty
Attempts:
3 left
💡 Hint
Common Mistakes
Using getValue which is not a WebElement method.
Using getText to get 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

  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