Verify login button is clickable using CSS selector
Preconditions (1)
✅ Expected Result: After clicking the login button, the user is redirected to the dashboard page with URL 'https://example.com/dashboard'
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.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.time.Duration; public class LoginButtonTest { private WebDriver driver; private WebDriverWait wait; @BeforeClass public void setUp() { // Set path to chromedriver if needed driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.get("https://example.com/login"); } @Test public void testLoginButtonClickable() { // Locate login button by CSS selector WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#loginBtn"))); // Assert button is displayed and enabled Assert.assertTrue(loginButton.isDisplayed(), "Login button should be visible"); Assert.assertTrue(loginButton.isEnabled(), "Login button should be enabled"); // Click the login button loginButton.click(); // Wait for URL to change to dashboard wait.until(ExpectedConditions.urlToBe("https://example.com/dashboard")); // Assert URL is correct Assert.assertEquals(driver.getCurrentUrl(), "https://example.com/dashboard", "URL should be dashboard after login"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
This test uses Selenium WebDriver with Java and TestNG framework.
In setUp(), we open the browser and navigate to the login page.
In the test method, we use By.cssSelector("#loginBtn") to find the login button by its CSS ID selector.
We use an explicit wait to wait until the button is clickable to avoid timing issues.
We assert the button is visible and enabled before clicking it.
After clicking, we wait until the URL changes to the dashboard URL and assert it matches the expected URL.
Finally, tearDown() closes the browser to clean up.
Now add data-driven testing with 3 different CSS selectors for different buttons on the page
driver.findElement(By.cssSelector(".btn-primary")) do in Selenium Java?findElement returns the first matching element found on the page..btn-primary matches elements with class btn-primary.#, so #login selects element with id 'login'.driver.findElement(By.cssSelector("#login")); is correct for Selenium Java.WebElement element = driver.findElement(By.cssSelector("div.content > p.highlight"));
String text = element.getText();
System.out.println(text);<div class="content"> <p class="highlight">Hello</p> <p>World</p> </div>
div.content > p.highlight selects a p element with class highlight that is a direct child of div with class content.p inside div.content has class highlight and text "Hello".WebElement button = driver.findElement(By.cssSelector("button#submit-btn.active"));
button.click();<button id="submit-btn">Submit</button>
button#submit-btn.active looks for a button with id submit-btn AND class active.submit-btn but no class active, so selector matches no element.input element inside a form with class 'search' that is enabled and visible. Which CSS selector is best to use with findElement in Selenium Java?:enabled pseudo-class for all browsers; instead, :not([disabled]) selects inputs without the disabled attribute.:enabled which is not widely supported in CSS selectors for Selenium.[disabled='false'] which is invalid because disabled is boolean attribute.:not([disabled]) which correctly selects enabled inputs.[visible='true'] which is not a valid attribute for visibility.Pass the test and this pattern moves unknown