0
0
Selenium Pythontesting~8 mins

CSS attribute selectors in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - CSS attribute selectors
Folder Structure
selenium-python-project/
├── src/
│   ├── pages/
│   │   ├── __init__.py
│   │   └── login_page.py
│   ├── tests/
│   │   ├── __init__.py
│   │   └── test_login.py
│   ├── utils/
│   │   ├── __init__.py
│   │   └── helpers.py
│   └── config/
│       ├── __init__.py
│       └── settings.py
├── requirements.txt
├── pytest.ini
└── README.md
    
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown using Selenium WebDriver.
  • Page Objects: Python classes representing web pages. Use CSS attribute selectors to locate elements precisely, e.g., input[type='email'].
  • Tests: Test scripts using pytest that call page object methods and assert expected behavior.
  • Utilities: Helper functions for common tasks like waits, logging, or data handling.
  • Configuration: Central place for environment variables, browser options, and credentials.
Configuration Patterns

Use a settings.py file to manage configurations:

# settings.py
ENV = 'staging'  # or 'production'
BROWSER = 'chrome'  # or 'firefox'
BASE_URL = {
    'staging': 'https://staging.example.com',
    'production': 'https://www.example.com'
}[ENV]

CREDENTIALS = {
    'username': 'testuser',
    'password': 'securepass'
}
    

Load these settings in tests and page objects to keep code flexible and avoid hardcoding.

Test Reporting and CI/CD Integration
  • Use pytest with pytest-html plugin to generate readable HTML reports after test runs.
  • Integrate tests into CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests automatically on code changes.
  • Configure reports to be archived or sent via email for quick feedback.
Best Practices
  1. Use CSS attribute selectors for precise element targeting: For example, button[type='submit'] targets submit buttons reliably.
  2. Keep selectors maintainable: Avoid overly complex selectors; prefer attributes that are stable and meaningful.
  3. Separate locators from test logic: Define all CSS selectors in page objects to centralize changes.
  4. Use explicit waits: Wait for elements located by attribute selectors to be visible or clickable before interacting.
  5. Parameterize selectors when needed: For example, use attribute selectors with variable values to reuse code for similar elements.
Self Check

Where in this folder structure would you add a new CSS attribute selector for a "Remember Me" checkbox on the login page?

Key Result
Organize Selenium Python tests with clear layers and use CSS attribute selectors in page objects for precise element location.