Test Overview
This test opens a browser, navigates to a sample page, finds a button using an XPath locator, clicks it, and verifies the expected text appears.
This test opens a browser, navigates to a sample page, finds a button using an XPath locator, 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 FindElementByXPathTest { private WebDriver driver; @BeforeEach public void setUp() { // Set the path to chromedriver executable if necessary // System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); } @Test public void testFindElementByXPath() { driver.get("https://example.com/testpage"); WebElement button = driver.findElement(By.xpath("//button[@id='submit-btn']")); button.click(); WebElement message = driver.findElement(By.xpath("//div[@id='message']")); assertEquals("Success", message.getText()); } @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 and ready | — | PASS |
| 2 | Navigates to https://example.com/testpage | Page loads with a button having id 'submit-btn' and a hidden message div | — | PASS |
| 3 | Finds button element using XPath //button[@id='submit-btn'] | Button element located on the page | Element is found and not null | PASS |
| 4 | Clicks the button element | Button click triggers message display | — | PASS |
| 5 | Finds message element using XPath //div[@id='message'] | Message element is visible with text 'Success' | Element text equals 'Success' | PASS |
| 6 | Test ends and browser closes | Browser window closed | — | PASS |
findElement(By.xpath("//button[@id='submit']")) do?findElement locates a single web element matching the given locator.//button[@id='submit'] selects the first <button> element with attribute id='submit'.driver.findElement(By.xpath("xpath_string")) with double quotes around the XPath string.WebElement element = driver.findElement(By.xpath("//div[@class='menu']/ul/li[2]"));
System.out.println(element.getText());
Assuming the HTML is:<div class='menu'>
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</div>//div[@class='menu']/ul/li[2] selects the second <li> child inside the <ul> within the div with class 'menu'.<li> inside the list is About. So element.getText() will print "About".NoSuchElementException. What is the likely error?
WebElement button = driver.findElement(By.xpath("//button[text()='Submit']"));
button.click();
The HTML is:
<button id='submitBtn'>Submit</button>
//button[text()='Submit'] matches the button text exactly, which is correct here.NoSuchElementException.name='email' or placeholder='Email'. Which XPath expression should you use with findElement?or between attribute checks. //input[@name='email' and @placeholder='Email'] uses and which requires both attributes. //input[contains(@name, 'email') or contains(@placeholder, 'Email')] uses contains which is broader but acceptable, but the question asks exact match. //input[@name='email' | @placeholder='Email'] uses invalid XPath syntax with | inside predicate.Pass the test and this pattern moves unknown