Test Overview
This test opens a web page, finds an element using a CSS selector, clicks it, and verifies the expected text appears. It checks that the CSS selector correctly locates the element.
This test opens a web page, finds an element using a CSS selector, clicks it, and verifies the expected text appears. It checks that the CSS selector correctly locates the element.
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import unittest class TestCssSelectorSyntax(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/testpage') def test_click_button_with_css_selector(self): driver = self.driver # Wait until the button with CSS selector is present button = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, 'button.submit-btn.primary')) ) button.click() # Verify the success message appears success_msg = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.CSS_SELECTOR, 'div.alert-success')) ) self.assertEqual(success_msg.text, 'Submission successful!') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open and ready | — | PASS |
| 2 | Browser navigates to 'https://example.com/testpage' | Page loads with a button having class 'submit-btn primary' | — | PASS |
| 3 | WebDriverWait waits up to 10 seconds for button with CSS selector 'button.submit-btn.primary' to be present | Button element is found in the DOM | Element located by CSS selector exists | PASS |
| 4 | Clicks the button found by CSS selector | Button is clicked, triggering page update | — | PASS |
| 5 | WebDriverWait waits up to 10 seconds for 'div.alert-success' to be visible | Success message element appears with text 'Submission successful!' | Text of success message equals 'Submission successful!' | PASS |
| 6 | Test ends and browser closes | Browser window closes | — | PASS |
button?. followed by the class name to select elements with that class.button.button selects all elements with class button.main-header?# followed by the ID name to select a unique element.main-header#main-header selects the element with ID main-header.<div class='container'> <button id='submit' class='btn primary'>Send</button> </div>
button element inside the div with class container?div.container button selects any button inside a div with class container.container > button.primary incorrectly targets a <container> tag (missing dot for class selector). #submit.container is invalid because ID and class cannot be combined like that without space. button#submit .container looks for .container inside button#submit, which is wrong.input elements with attribute type='text'. Which CSS selector is correct?input[type=text] and input[type='text'] are valid.input[type="text"] is also valid. So all three selectors correctly select input elements with type='text'.li elements that are the first child of their parent ul. Which CSS selector should you use?ul > li:first-child selects li elements that are the first child directly under ul.ul li:first-child selects any li that is first child of its parent inside any ul descendant (could be nested). li:first-child selects any li that is first child anywhere. ul li:first-of-type selects first li of its type inside ul, but not necessarily first child.Pass the test and this pattern moves unknown