0
0
Selenium Pythontesting~10 mins

Find element by CSS selector in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page and finds a button using a CSS selector. It then clicks the button and verifies the button's text changes as expected.

Test Code - Selenium
Selenium Python
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 TestFindElementByCssSelector(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/button')

    def test_click_button_by_css_selector(self):
        driver = self.driver
        # Wait until button is present
        button = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, 'button.submit-btn'))
        )
        button.click()
        # Verify button text changed
        updated_text = WebDriverWait(driver, 10).until(
            EC.text_to_be_present_in_element((By.CSS_SELECTOR, 'button.submit-btn'), 'Clicked')
        )
        self.assertTrue(updated_text)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open and ready-PASS
2Browser navigates to 'https://example.com/button'Page with a button having class 'submit-btn' is loaded-PASS
3Waits up to 10 seconds for button with CSS selector 'button.submit-btn' to be presentButton element is found on the pageElement located by CSS selector 'button.submit-btn' existsPASS
4Clicks the button found by CSS selectorButton is clicked, triggering text change-PASS
5Waits up to 10 seconds for button text to change to include 'Clicked'Button text updated to 'Clicked' or contains 'Clicked'Button text contains 'Clicked'PASS
6Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: Button with CSS selector 'button.submit-btn' is not found within 10 seconds
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test wait for before clicking the button?
APresence of the button element located by CSS selector
BPage title to contain 'Button'
CButton to be visible and enabled
DURL to change after clicking
Key Result
Always wait explicitly for elements using reliable locators like CSS selectors before interacting, to avoid flaky tests.