Verify login button presence using XPath and CSS selectors
Preconditions (1)
✅ Expected Result: The login button is found and visible using both XPath and CSS selectors
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 LoginButtonTest { private WebDriver driver; private WebDriverWait wait; @BeforeEach public void setUp() { // Set path to chromedriver executable if needed driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.get("https://example.com/login"); } @Test public void testLoginButtonUsingXPath() { By loginButtonXPath = By.xpath("//button[@id='loginBtn']"); WebElement loginButton = wait.until(ExpectedConditions.visibilityOfElementLocated(loginButtonXPath)); assertTrue(loginButton.isDisplayed(), "Login button should be visible using XPath"); } @Test public void testLoginButtonUsingCssSelector() { By loginButtonCss = By.cssSelector("button#loginBtn"); WebElement loginButton = wait.until(ExpectedConditions.visibilityOfElementLocated(loginButtonCss)); assertTrue(loginButton.isDisplayed(), "Login button should be visible using CSS selector"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
This test class uses Selenium WebDriver with Java and JUnit 5.
setUp() method initializes the ChromeDriver and opens the login page URL.
Two test methods verify the login button presence:
testLoginButtonUsingXPath() locates the button by XPath //button[@id='loginBtn'] and waits until it is visible, then asserts it is displayed.testLoginButtonUsingCssSelector() locates the same button by CSS selector button#loginBtn with explicit wait and asserts visibility.Explicit waits ensure the test waits for the element to appear instead of failing immediately.
tearDown() closes the browser after each test.
This approach shows how to choose between XPath and CSS selectors for locating elements, emphasizing best practices like explicit waits and clear assertions.
Now add data-driven testing with 3 different login button IDs to verify presence using both XPath and CSS selectors.
<div> elements with class active?div.active selects divs with class active.<ul> <li class='item'>One</li> <li class='item'>Two</li> <li class='item'>Three</li> </ul>
List<WebElement> elements = driver.findElements(By.cssSelector("ul > li.item"));
System.out.println(elements.size());ul > li.item selects all li elements with class 'item' that are direct children of ul.li elements with class 'item' directly under ul, so the list size is 3.By.xpath("//div[@id='main']//a[text()='Click Here']")a element with exact text 'Click Here' inside a div with id 'main'.<li> element inside a <ul> with dynamic classes and no IDs. Which locator strategy is best and why?li elements have dynamic classes and no IDs, so ID locator is not possible.li, but CSS :nth-child(3) is simpler and faster for this case.Pass the test and this pattern moves unknown