Test Overview
This test opens a web page and uses XPath functions contains and starts-with to find elements by partial attribute values. It verifies that the correct elements are found and clickable.
This test opens a web page and uses XPath functions contains and starts-with to find elements by partial attribute values. It verifies that the correct elements are found and clickable.
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 XPathFunctionsTest { WebDriver driver; WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void testContainsAndStartsWithXPath() { driver.get("https://example.com/testpage"); // Find element with id containing 'submit' WebElement submitButton = wait.until( ExpectedConditions.presenceOfElementLocated(By.xpath("//button[contains(@id, 'submit')]") ) ); assertTrue(submitButton.isDisplayed()); // Find element with class starting with 'nav-' WebElement navMenu = wait.until( ExpectedConditions.presenceOfElementLocated(By.xpath("//nav[starts-with(@class, 'nav-')]") ) ); assertTrue(navMenu.isDisplayed()); // Click the submit button submitButton.click(); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open, ready to navigate | — | PASS |
| 2 | Navigate to https://example.com/testpage | Browser loads the test page with buttons and navigation menu | — | PASS |
| 3 | Wait until button with id containing 'submit' is present using XPath contains() | Button element with id like 'btn-submit' is found in DOM | Check that submit button is displayed | PASS |
| 4 | Wait until nav element with class starting with 'nav-' is present using XPath starts-with() | Navigation element with class like 'nav-main' is found in DOM | Check that navigation menu is displayed | PASS |
| 5 | Click the submit button | Submit button is clicked, triggering form submission or action | — | PASS |
| 6 | Test ends and browser closes | Browser window closes cleanly | — | PASS |
contains()contains() function checks if an attribute or text contains a given substring anywhere inside it.starts-with() checks only the beginning, ends-with() is not standard XPath 1.0, and equals() is not an XPath function.id attribute starts with "submit"?starts-with() is used to check if an attribute starts with a substring.//button[starts-with(@id, 'submit')] is correct XPath syntax. Options B and C use invalid XPath operators.<input type="text" id="user_email" value="test@example.com"/>
//*[@id[contains(., 'user')]]//*[@id[contains(., 'user')]] is syntactically incorrect because @id is an attribute node and cannot be used as a function or have predicates applied directly like this.id attribute contains 'user', the correct XPath is //*[@id[contains(., 'user')]] is invalid; instead use //*[contains(@id, 'user')].//a[contains(starts-with(@href, 'http'))]contains() function expects two string arguments, but here it wraps starts-with() which returns a boolean, causing a syntax error.starts-with(@href, 'http') alone or contains(@href, 'http'), but not nested.<div> elements whose class attribute starts with "alert" but excludes those that contain "dismiss" anywhere in the class. Which XPath expression correctly achieves this?starts-with(@class, 'alert') selects elements whose class attribute begins with "alert".not(contains(@class, 'dismiss')) excludes elements whose class contains "dismiss" anywhere.and to combine them.Pass the test and this pattern moves unknown