0
0
Selenium Javatesting~15 mins

XPath with attributes in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify login button is clickable using XPath with attributes
Preconditions (1)
Step 1: Locate the login button using XPath with attribute 'id' equal to 'loginBtn'
Step 2: Click the login button
Step 3: Verify that the URL changes to 'https://example.com/dashboard'
✅ Expected Result: After clicking the login button located by XPath with attribute, the user is redirected to the dashboard page with URL 'https://example.com/dashboard'
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify the login button is found using XPath with attribute
Verify the login button is clickable
Verify the URL after clicking the button is 'https://example.com/dashboard'
Best Practices:
Use explicit waits to wait for the login button to be clickable
Use By.xpath locator with attribute syntax
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 LoginButtonXPathTest {
    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.manage().window().maximize();
        driver.get("https://example.com/login");
    }

    @Test
    public void testLoginButtonClickableUsingXPathAttribute() {
        // Locate login button using XPath with attribute id='loginBtn'
        By loginButtonLocator = By.xpath("//button[@id='loginBtn']");

        // Wait until the login button is clickable
        WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(loginButtonLocator));

        // Assert the button is displayed
        Assert.assertTrue(loginButton.isDisplayed(), "Login button should be visible");

        // Click the login button
        loginButton.click();

        // Wait for URL to change to dashboard
        wait.until(ExpectedConditions.urlToBe("https://example.com/dashboard"));

        // Assert the URL is correct
        String currentUrl = driver.getCurrentUrl();
        Assert.assertEquals(currentUrl, "https://example.com/dashboard", "URL should be dashboard after login");
    }

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

This test uses Selenium WebDriver with Java and TestNG framework.

In setUp(), we open the Chrome browser and navigate to the login page.

In the test method, we locate the login button using XPath with an attribute id='loginBtn'. This is done with By.xpath("//button[@id='loginBtn']").

We use an explicit wait to wait until the button is clickable to avoid timing issues.

We assert the button is visible, then click it.

After clicking, we wait until the URL changes to the dashboard URL and assert it matches the expected URL.

Finally, in tearDown(), we close the browser to clean up.

This structure ensures the test is stable, readable, and follows best practices.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
{'mistake': "Using incorrect XPath syntax like //button[id='loginBtn']", 'why_bad': "This XPath is invalid because attribute selectors require '@' before the attribute name.", 'correct_approach': "Use correct XPath syntax: //button[@id='loginBtn']"}
Not verifying the element is visible before clicking
Hardcoding URLs without waiting for navigation
Bonus Challenge

Now add data-driven testing with 3 different login button IDs to verify the button is clickable for each.

Show Hint