Bird
Raised Fist0
Selenium Javatesting~5 mins

Getting text and attributes in Selenium Java - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What method in Selenium Java is used to get the visible text of a web element?
The getText() method is used to retrieve the visible text of a web element.
Click to reveal answer
beginner
How do you get the value of an attribute from a web element in Selenium Java?
Use the getAttribute(String attributeName) method, passing the attribute name as a string.
Click to reveal answer
intermediate
What will element.getText() return if the element is hidden on the page?
It will return an empty string because getText() only returns visible text.
Click to reveal answer
intermediate
Why might getAttribute("value") be preferred over getText() for input fields?
Because input fields often store their content in the "value" attribute, not as visible text, so getAttribute("value") retrieves the entered text.
Click to reveal answer
beginner
What is the difference between getText() and getAttribute() in Selenium?
getText() returns the visible text inside an element. getAttribute() returns the value of a specified attribute of the element, which may not be visible text.
Click to reveal answer
Which Selenium Java method retrieves the visible text of a web element?
AgetContent()
BgetAttribute()
CgetText()
DgetValue()
How do you get the 'href' attribute of a link element in Selenium Java?
Aelement.getLink()
Belement.getText()
Celement.getValue("href")
Delement.getAttribute("href")
What does element.getText() return if the element is not visible?
AThe text inside the element
BEmpty string
Cnull
DThrows an exception
Which method is best to get the typed text inside an input field?
AgetAttribute("value")
BgetText()
CgetInput()
DgetContent()
What is the return type of getAttribute() in Selenium Java?
AString
Bint
Cboolean
Dvoid
Explain how to retrieve visible text and attribute values from web elements using Selenium Java.
Think about what you see on the page versus what is stored in element attributes.
You got /4 concepts.
    Describe a scenario where getText() might not return the expected text and how to handle it.
    Consider input fields and hidden elements.
    You got /4 concepts.

      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