Test Overview
This test opens a web page, finds a button using a CSS selector, clicks it, and verifies the button's text changes as expected.
This test opens a web page, finds a button using a CSS selector, clicks it, and verifies the button's text changes as expected.
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 CssSelectorTest { private WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); } @Test public void testFindElementByCssSelector() { driver.get("https://example.com/buttonpage"); WebElement button = driver.findElement(By.cssSelector("button#submit-btn.primary")); button.click(); String buttonText = button.getText(); assertEquals("Submitted", buttonText); } @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 | Navigate to URL https://example.com/buttonpage | Browser displays the page with a button having id 'submit-btn' and class 'primary' | — | PASS |
| 3 | Find element using CSS selector 'button#submit-btn.primary' | Button element located on the page | Element is found and is a button with correct id and class | PASS |
| 4 | Click the found button element | Button is clicked, triggering page or element update | — | PASS |
| 5 | Get text of the button after click | Button text is now 'Submitted' | Assert that button text equals 'Submitted' | PASS |
| 6 | Test ends and browser closes | Browser window is closed | — | PASS |
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