We get text and attributes from web elements to check if the page shows the right information or has the correct settings.
Getting text and attributes in Selenium Java
String text = driver.findElement(By.locator).getText();
String attributeValue = driver.findElement(By.locator).getAttribute("attributeName");getText() returns the visible text inside an element.
getAttribute() returns the value of the given attribute, like href, src, or value.
String buttonText = driver.findElement(By.id("submitBtn")).getText();String linkHref = driver.findElement(By.cssSelector("a.home-link")).getAttribute("href");
String inputValue = driver.findElement(By.name("username")).getAttribute("value");
This test opens example.com, gets the main heading text and a link's href attribute, prints them, and checks if they match expected values.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class GetTextAndAttributesTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com"); WebElement heading = driver.findElement(By.tagName("h1")); String headingText = heading.getText(); WebElement link = driver.findElement(By.cssSelector("p a")); String linkHref = link.getAttribute("href"); System.out.println("Heading text: " + headingText); System.out.println("Link href: " + linkHref); assert headingText.equals("Example Domain") : "Heading text does not match!"; assert linkHref.startsWith("https://") : "Link href is not a valid URL!"; System.out.println("All assertions passed."); } finally { driver.quit(); } } }
Always wait for elements to be visible before getting text or attributes to avoid errors.
getText() returns visible text only, not hidden or attribute text.
getAttribute() returns null if the attribute does not exist on the element.
Use getText() to read visible text from elements.
Use getAttribute() to read any attribute value from elements.
Check the values you get to confirm the page shows what you expect.
Practice
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 BQuick Check:
Visible text = getText() [OK]
- Confusing getText() with getAttribute()
- Using getValue() which is not a Selenium method
- Trying to get text with getContent() which doesn't exist
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 AQuick Check:
Attribute value = getAttribute("name") [OK]
- Using getText() to get attribute values
- Calling non-existent methods like getHref()
- Passing attribute name as parameter to getText()
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?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 DQuick Check:
Text + "," + Attribute = Submit,button [OK]
- Mixing order of printed values
- Expecting getText() to return attribute value
- Ignoring the comma in print output
WebElement input = driver.findElement(By.name("email"));
String placeholder = input.getText();
But placeholder is empty. What is the error and how to fix it?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 AQuick Check:
Attributes need getAttribute() [OK]
- Using getText() to get attribute values
- Trying to pass attribute name to getText()
- Using non-existent getValue() method
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 CQuick Check:
Text = getText(), Attribute = getAttribute() [OK]
- Using getAttribute("text") which is invalid
- Comparing href exactly instead of contains
- Mixing up text and href in assertions
Ready to prove it?
Pass the test and this pattern moves unknown
