Getting text and attributes in Selenium Java - Build an Automation Script
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.time.Duration; public class GetTextAndAttributesTest { private WebDriver driver; private WebDriverWait wait; @BeforeClass public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.manage().window().maximize(); } @Test public void testGetTextAndAttributes() { driver.get("http://example.com/sample"); WebElement heading = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("main-heading"))); String headingText = heading.getText(); Assert.assertEquals(headingText, "Welcome to Sample Page", "Heading text does not match"); WebElement submitButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("submit-btn"))); String buttonType = submitButton.getAttribute("type"); Assert.assertEquals(buttonType, "submit", "Button type attribute does not match"); WebElement helpLink = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("help-link"))); String hrefValue = helpLink.getAttribute("href"); Assert.assertEquals(hrefValue, "http://example.com/help", "Link href attribute does not match"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
The @BeforeClass method sets up the ChromeDriver and WebDriverWait to wait up to 10 seconds for elements.
The test method navigates to the sample page URL.
It waits explicitly for the heading element to be visible, then gets its text and asserts it matches the expected string.
Similarly, it waits for the submit button and gets its type attribute, asserting it equals 'submit'.
Then it waits for the help link, gets its href attribute, and asserts it matches the expected URL.
The @AfterClass method closes the browser to clean up.
This approach uses explicit waits to avoid timing issues, uses By.id locators for clarity and speed, and TestNG assertions for clear validation messages.
Now add data-driven testing with 3 different sets of expected heading text, button type, and link href values
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
