Bird
Raised Fist0
Selenium Javatesting~10 mins

Getting text and attributes in Selenium Java - Test Execution Trace

Choose your learning style10 modes available
Test Overview

This test opens a web page, finds a button element, retrieves its visible text and the value of its 'type' attribute, and verifies both values are as expected.

Test Code - JUnit with Selenium WebDriver
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class GetTextAndAttributeTest {
    WebDriver driver;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
    }

    @Test
    public void testButtonTextAndTypeAttribute() {
        driver.get("https://example.com/buttonpage");
        WebElement button = driver.findElement(By.id("submit-btn"));
        String buttonText = button.getText();
        String buttonType = button.getAttribute("type");
        assertEquals("Submit", buttonText);
        assertEquals("button", buttonType);
    }

    @AfterEach
    public void tearDown() {
        driver.quit();
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready—PASS
2Navigates to URL 'https://example.com/buttonpage'Browser displays the page with a button having id 'submit-btn'—PASS
3Finds the button element by id 'submit-btn'Button element located on the page—PASS
4Retrieves visible text from the button elementButton text is read as 'Submit'Verify button text equals 'Submit'PASS
5Retrieves 'type' attribute value from the button elementButton attribute 'type' read as 'button'Verify button attribute 'type' equals 'button'PASS
6Test ends and browser closesBrowser window closed—PASS
Failure Scenario
Failing Condition: Button element with id 'submit-btn' is not found or text/attribute values do not match expected
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after finding the button element?
AThe visible text and the 'type' attribute of the button
BOnly the button's CSS styles
CThe button's position on the page
DThe button's click functionality
Key Result
Always verify both visible text and relevant attributes of elements to ensure the UI shows correct content and properties.

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