0
0
Selenium Javatesting~10 mins

Choosing XPath vs CSS strategy in Selenium Java - Test Execution Compared

Choose your learning style9 modes available
Test Overview

This test opens a webpage and tries to find a button using both XPath and CSS selectors. It verifies that the button is clickable and visible, demonstrating the difference in locating elements with XPath and CSS strategies.

Test Code - JUnit with Selenium WebDriver
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.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.time.Duration;

public class XPathVsCssTest {
    WebDriver driver;
    WebDriverWait wait;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    @Test
    public void testButtonWithXPathAndCss() {
        driver.get("https://example.com/testpage");

        // Locate button using XPath
        WebElement buttonByXPath = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='submit-btn']")));
        assertTrue(buttonByXPath.isDisplayed(), "Button located by XPath should be visible");

        // Locate button using CSS Selector
        WebElement buttonByCss = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#submit-btn")));
        assertTrue(buttonByCss.isDisplayed(), "Button located by CSS should be visible");

        // Click the button using CSS selector
        buttonByCss.click();
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Navigates to https://example.com/testpagePage loads with a button having id 'submit-btn'-PASS
3Waits until button located by XPath //button[@id='submit-btn'] is clickableButton is present and clickable on the pageVerify button located by XPath is displayedPASS
4Asserts button located by XPath is visibleButton is visibleassertTrue(buttonByXPath.isDisplayed())PASS
5Waits until button located by CSS selector button#submit-btn is clickableButton is present and clickable on the pageVerify button located by CSS is displayedPASS
6Asserts button located by CSS selector is visibleButton is visibleassertTrue(buttonByCss.isDisplayed())PASS
7Clicks the button located by CSS selectorButton click triggers expected action-PASS
8Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: Button with id 'submit-btn' is not found or not clickable within 10 seconds
Execution Trace Quiz - 3 Questions
Test your understanding
Which locator strategy is used to find the button first in the test?
ACSS Selector
BXPath
CID locator
DClass name
Key Result
Use explicit waits with clear locator strategies like XPath or CSS selectors to reliably find elements. Choose CSS selectors for speed and simplicity when possible, XPath for complex queries.