Locate and verify the login button using XPath
Preconditions (3)
✅ Expected Result: The login button is found using XPath and is visible on the page
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 java.time.Duration; import static org.junit.jupiter.api.Assertions.assertTrue; public class FindElementByXPathTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com/login"); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); WebElement loginButton = wait.until( ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@id='loginBtn']")) ); assertTrue(loginButton.isDisplayed(), "Login button should be visible"); } finally { driver.quit(); } } }
This code starts by setting up the Chrome WebDriver and opening the login page URL.
It uses an explicit wait to wait up to 10 seconds for the login button to be visible, locating it by the XPath expression //button[@id='loginBtn'].
Then it asserts that the button is displayed on the page, ensuring the element was found and is visible.
Finally, it closes the browser to clean up.
This approach avoids timing issues by waiting explicitly and uses a clear XPath locator for maintainability.
Now add data-driven testing to locate and verify buttons with three different XPath expressions on the same page.
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