0
0
Selenium Pythontesting~8 mins

CSS pseudo-classes for selection in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - CSS pseudo-classes for selection
Folder Structure
selenium-python-project/
├── src/
│   ├── pages/
│   │   ├── __init__.py
│   │   └── home_page.py          # Page objects using CSS pseudo-classes
│   ├── tests/
│   │   ├── __init__.py
│   │   └── test_home_page.py     # Test cases using page objects
│   ├── utils/
│   │   ├── __init__.py
│   │   └── wait_helpers.py       # Explicit wait utilities
│   └── config/
│       ├── __init__.py
│       └── settings.py           # Configurations for environment, browser
├── requirements.txt
├── pytest.ini                    # Pytest configuration
└── README.md
    
Test Framework Layers
  • Config Layer: Holds environment variables, browser options, and credentials in config/settings.py.
  • Page Object Layer: Classes in src/pages/ represent web pages. They use CSS selectors with pseudo-classes like :nth-child(), :first-child, :last-child to locate elements precisely.
  • Utility Layer: Helper functions for waits and common Selenium actions in src/utils/wait_helpers.py.
  • Test Layer: Test scripts in src/tests/ use page objects to perform actions and assertions.
Configuration Patterns

Use config/settings.py to store:

  • Base URLs for different environments (dev, staging, prod).
  • Browser choice (Chrome, Firefox) with options.
  • Timeout values for waits.
  • Credentials securely (use environment variables or secrets manager in real projects).

Example snippet from settings.py:

import os

BASE_URL = "https://example.com"
BROWSER = "chrome"
IMPLICIT_WAIT = 10
EXPLICIT_WAIT = 15
# Credentials loaded from environment variables for safety
USERNAME = os.getenv('APP_USERNAME')
PASSWORD = os.getenv('APP_PASSWORD')
Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html to generate readable HTML reports.
  • Configure CI pipelines (GitHub Actions, Jenkins) to run tests on code push.
  • Fail fast on critical test failures to save time.
  • Store reports as artifacts in CI for review.
  • Use screenshots on failure to help debugging.
Best Practices for CSS Pseudo-Classes in Selenium Python Framework
  1. Use CSS pseudo-classes to target elements precisely: For example, li:nth-child(2) selects the second list item, helping avoid brittle XPath expressions.
  2. Combine pseudo-classes with classes or IDs: This improves selector stability, e.g., ul.menu > li:first-child.
  3. Prefer explicit waits over implicit waits: Wait for elements to be visible or clickable before interacting.
  4. Keep selectors readable and maintainable: Avoid overly complex selectors that are hard to understand.
  5. Organize selectors in page objects: Centralize CSS selectors so updates are easy when UI changes.
Self Check

Where in this folder structure would you add a new page object for a login page that uses CSS pseudo-classes to select input fields?

Key Result
Organize Selenium Python tests with page objects using CSS pseudo-classes for precise element selection.