0
0
Selenium Javatesting~15 mins

XPath with text() in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify button click using XPath with text()
Preconditions (2)
Step 1: Locate the button with the exact text 'Submit' using XPath with text() function.
Step 2: Click the 'Submit' button.
Step 3: Verify that clicking the button leads to the expected page or changes the page content accordingly.
✅ Expected Result: The 'Submit' button is clicked successfully using XPath with text(), and the expected page or content change occurs.
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify the 'Submit' button is found using XPath with text()
Verify the button click leads to expected page or content change
Best Practices:
Use explicit waits to wait for the button to be clickable
Use By.xpath with text() function for locating the button
Use assertions from TestNG or JUnit 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 XPathTextTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeClass
    public void setUp() {
        // Set path to chromedriver if needed
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.get("https://example.com/sample-buttons"); // Replace with actual URL
    }

    @Test
    public void testClickSubmitButtonUsingXPathText() {
        // Locate the button with exact text 'Submit' using XPath with text()
        By submitButtonLocator = By.xpath("//button[text()='Submit']");

        // Wait until the button is clickable
        WebElement submitButton = wait.until(ExpectedConditions.elementToBeClickable(submitButtonLocator));

        // Verify the button is displayed
        Assert.assertTrue(submitButton.isDisplayed(), "Submit button should be visible");

        // Click the button
        submitButton.click();

        // Verify expected result - for example, URL changed or confirmation message displayed
        // Here we check URL contains 'submitted' as an example
        wait.until(ExpectedConditions.urlContains("submitted"));
        String currentUrl = driver.getCurrentUrl();
        Assert.assertTrue(currentUrl.contains("submitted"), "URL should contain 'submitted' after clicking Submit");
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test script uses Selenium WebDriver with Java and TestNG framework.

Setup: The @BeforeClass method initializes the ChromeDriver and opens the sample page.

Test: We locate the 'Submit' button using XPath with the text() function: //button[text()='Submit']. We use an explicit wait to ensure the button is clickable before interacting.

We assert the button is visible, then click it. After clicking, we wait for the URL to contain the word 'submitted' to confirm the expected page change.

Teardown: The @AfterClass method closes the browser to clean up.

This approach ensures the test is stable, readable, and follows best practices like explicit waits and proper assertions.

Common Mistakes - 4 Pitfalls
Using XPath without text() and relying on partial matches
Not using explicit waits before clicking the button
Hardcoding URLs or page content without verification
Not closing the browser after test execution
Bonus Challenge

Now add data-driven testing to click buttons with text 'Submit', 'Cancel', and 'Reset' using XPath with text()

Show Hint