Test Overview
This test opens a webpage and uses CSS attribute and pseudo-class selectors to find elements. It verifies that the correct elements are found and interactable.
This test opens a webpage and uses CSS attribute and pseudo-class selectors to find elements. It verifies that the correct elements are found and interactable.
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.*; import java.time.Duration; public class CssSelectorTest { WebDriver driver; WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void testCssAttributeAndPseudoClassSelectors() { driver.get("https://example.com/testpage"); // Find input with attribute type='text' WebElement inputText = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("input[type='text']"))); assertNotNull(inputText); // Find link with href containing 'contact' WebElement contactLink = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("a[href*='contact']"))); assertTrue(contactLink.isDisplayed()); // Find first list item using :first-child pseudo-class WebElement firstListItem = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("ul#menu li:first-child"))); assertEquals("Home", firstListItem.getText()); // Click the contact link contactLink.click(); // Verify URL changed to contact page wait.until(ExpectedConditions.urlContains("contact")); assertTrue(driver.getCurrentUrl().contains("contact")); } @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 https://example.com/testpage | Browser displays the test page with inputs, links, and menu | — | PASS |
| 3 | Find input element with attribute type='text' using CSS selector input[type='text'] | Input element is present in the DOM | Assert inputText is not null | PASS |
| 4 | Find link element with href containing 'contact' using CSS selector a[href*='contact'] | Contact link is present and visible | Assert contactLink is displayed | PASS |
| 5 | Find first list item in menu using CSS selector ul#menu li:first-child | First list item is present with text 'Home' | Assert firstListItem text equals 'Home' | PASS |
| 6 | Click the contact link | Browser navigates to contact page | — | PASS |
| 7 | Wait until URL contains 'contact' | URL is updated to contact page URL | Assert current URL contains 'contact' | PASS |
| 8 | Test ends and browser closes | Browser window is closed | — | PASS |
<input> elements with a type attribute equal to "text"?input[type='text'] matches input elements whose type attribute exactly equals text.^= means starts with, $= means ends with, and *= means contains. These do not require exact match.<a> elements with an href attribute containing the word "login"?a[href*='login'] matches a elements whose href attribute contains the substring login anywhere.^= matches start, $= matches end, and = matches exact. We want contains, so *= is correct.<ul> <li>Item 1</li> <li class="active">Item 2</li> <li>Item 3</li> <li class="active">Item 4</li> </ul>
li.active:first-of-type select?:first-of-type pseudo-class:first-of-type selects the first sibling of its type (here, the first li) regardless of class.li.active:first-of-type looks for an li that is both .active and the first li. The first li has no class, so no match.:first-of-type matches first element type ignoring classes [OK]<button> elements that are disabled using a CSS selector in Selenium. Which selector is incorrect and will cause your test to fail?button:disabled and button[disabled] correctly select disabled buttons. button[disabled='disabled'] also works if attribute equals 'disabled'.:enable is not a valid CSS pseudo-class. The correct one is :enabled.<input> elements with various name attributes:<input name="user_email"> <input name="user_password"> <input name="admin_email"> <input name="admin_password">
input elements whose name attribute ends with "_email"?input[name$='_email'] matches input elements whose name attribute ends exactly with _email.*= matches contains anywhere, ^= matches starts with, and = matches exact. We want ends with, so $= is correct.Pass the test and this pattern moves unknown