0
0
Selenium Pythontesting~8 mins

CSS selector syntax in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - CSS selector syntax
Folder Structure
selenium-python-project/
├── src/
│   ├── pages/
│   │   └── login_page.py
│   ├── tests/
│   │   └── test_login.py
│   ├── utils/
│   │   └── selectors.py
│   └── config/
│       └── config.py
├── requirements.txt
└── pytest.ini
    
Test Framework Layers
  • Driver Layer: Manages Selenium WebDriver setup and teardown (e.g., in conftest.py or fixtures).
  • Page Objects: Python classes representing web pages, using CSS selectors to locate elements.
  • Tests: Test scripts using pytest that call page object methods and assert behaviors.
  • Utilities: Helper functions or constants, such as CSS selector strings centralized in selectors.py.
  • Configuration: Environment settings, URLs, credentials stored in config.py for easy management.
Configuration Patterns

Use a config.py file to store environment variables like base URLs and browser types.

Example:

BASE_URL = "https://example.com"
BROWSER = "chrome"
TIMEOUT = 10

Use pytest command-line options or environment variables to switch environments or browsers.

Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html to generate readable HTML reports.
  • Integrate tests into CI/CD pipelines (GitHub Actions, Jenkins) to run tests on each commit.
  • Configure reports to be archived or sent via email for quick feedback.
Best Practices for CSS Selector Syntax in Selenium Python Framework
  • Use unique and stable CSS selectors to avoid flaky tests (e.g., IDs or data-* attributes).
  • Centralize CSS selectors in a utility file or page object to ease maintenance.
  • Prefer simple selectors over complex chains for readability and speed.
  • Use explicit waits with selectors to ensure elements are ready before interaction.
  • Validate selectors in browser DevTools before using them in code.
Self-Check Question

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

Key Result
Organize Selenium Python tests with clear folder layers, centralized CSS selectors, and robust configuration for maintainable automation.