Test Overview
This test opens a web page, finds a button using XPath with an attribute filter, clicks it, and verifies the expected text appears.
This test opens a web page, finds a button using XPath with an attribute filter, clicks it, and verifies the expected text appears.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; 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.assertEquals; public class XPathAttributeTest { WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); } @Test public void testButtonClickByXPathAttribute() { driver.get("https://example.com/testpage"); WebElement button = driver.findElement(By.xpath("//button[@id='submit-btn']")); button.click(); WebElement message = driver.findElement(By.id("result-message")); assertEquals("Success", message.getText()); } @AfterEach public void tearDown() { driver.quit(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open and ready | — | PASS |
| 2 | Navigates to https://example.com/testpage | Page loads with a button having id 'submit-btn' and a hidden result message | — | PASS |
| 3 | Finds button using XPath //button[@id='submit-btn'] | Button element located successfully | Element found matches XPath with attribute id='submit-btn' | PASS |
| 4 | Clicks the button | Button click triggers page update showing result message | — | PASS |
| 5 | Finds element with id 'result-message' | Result message element is visible with text 'Success' | Element text equals 'Success' | PASS |
| 6 | Test ends and browser closes | Browser window closed | — | PASS |
//input[@type='submit'] select in a web page?//input to select all <input> tags anywhere in the document.[@type='submit'] selects only those <input> elements whose 'type' attribute exactly equals 'submit'.@attributeName='value' inside square brackets.@id='loginBtn' correctly. Options A, B, and D use invalid syntax for attributes.<input type='checkbox' name='subscribe' checked>
//input[@checked] select?//input[@checked] selects all <input> elements that have the 'checked' attribute, regardless of its value.//div[@class='container'] but it does not find the element. The element's class attribute is "container main". What is the issue?@class='container' matches only if the class attribute is exactly 'container'.<input> elements that have a type attribute equal to 'text' or 'password' using XPath. Which expression is correct?or between attribute tests.or to select inputs with type 'text' or 'password'. //input[@type='text' and @type='password'] uses and which is impossible for one attribute to have two values simultaneously. Options C and D use invalid syntax.Pass the test and this pattern moves unknown