0
0
Selenium Pythontesting~8 mins

Find element by CSS selector in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Find element by CSS selector
Folder Structure
selenium-python-project/
├── tests/
│   ├── test_login.py
│   └── test_search.py
├── pages/
│   ├── base_page.py
│   └── login_page.py
├── utils/
│   └── helpers.py
├── config/
│   ├── config.yaml
│   └── credentials.yaml
├── conftest.py
└── requirements.txt
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown using Selenium WebDriver in conftest.py.
  • Page Objects: Classes in pages/ folder encapsulate page elements and actions. Use CSS selectors to find elements.
  • Tests: Test scripts in tests/ folder use page objects to perform actions and assertions.
  • Utilities: Helper functions in utils/ for reusable code like waits or logging.
  • Configuration: Store environment settings and credentials in config/ files.
Configuration Patterns
  • Use config/config.yaml to define environment URLs and browser types.
  • Store sensitive data like usernames and passwords in config/credentials.yaml, not in code.
  • Load configuration in conftest.py to initialize WebDriver with chosen browser and base URL.
  • Use command line options or environment variables to switch environments (e.g., dev, staging, prod).
Test Reporting and CI/CD Integration
  • Use pytest as test runner with built-in reporting.
  • Generate HTML reports with pytest-html plugin for easy viewing.
  • Integrate tests in CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run on code push.
  • Fail tests if elements are not found by CSS selector, providing clear error messages.
Best Practices
  • Use meaningful, stable CSS selectors that are unlikely to change often.
  • Encapsulate element locators inside page object classes to keep tests clean.
  • Use explicit waits to wait for elements located by CSS selector to be visible or clickable.
  • Keep selectors simple and avoid overly complex CSS expressions.
  • Separate test data and configuration from test code for flexibility.
Self Check

Where in this folder structure would you add a new CSS selector locator for a "Submit" button on the login page?

Key Result
Use Page Object Model with CSS selectors encapsulated in page classes, supported by config and utilities for robust Selenium Python tests.