0
0
Selenium Javatesting~15 mins

Getting text and attributes in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify text and attribute values of elements on the sample page
Preconditions (2)
Step 1: Open the browser and navigate to http://example.com/sample
Step 2: Locate the heading element with id 'main-heading' and get its visible text
Step 3: Locate the button element with id 'submit-btn' and get its 'type' attribute
Step 4: Locate the link element with id 'help-link' and get its 'href' attribute
Step 5: Verify the heading text is exactly 'Welcome to Sample Page'
Step 6: Verify the button 'type' attribute is 'submit'
Step 7: Verify the link 'href' attribute is 'http://example.com/help'
✅ Expected Result: The heading text matches 'Welcome to Sample Page', the button's type attribute is 'submit', and the link's href attribute is 'http://example.com/help'
Automation Requirements - Selenium WebDriver with Java and TestNG
Assertions Needed:
Assert heading text equals 'Welcome to Sample Page'
Assert button 'type' attribute equals 'submit'
Assert link 'href' attribute equals 'http://example.com/help'
Best Practices:
Use explicit waits to ensure elements are present before accessing
Use By.id locator strategy for element identification
Use TestNG assertions for validation
Close the browser after test execution
Automated Solution
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.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.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
Using incorrect locator strategies like XPath with absolute paths
Not closing the browser after test execution
Asserting on getText() without trimming or checking for case sensitivity
Bonus Challenge

Now add data-driven testing with 3 different sets of expected heading text, button type, and link href values

Show Hint