0
0
Selenium Pythontesting~8 mins

Finding multiple elements in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Finding multiple elements
Folder Structure
selenium-python-project/
├── tests/
│   ├── test_example.py
│   └── test_elements.py
├── pages/
│   ├── base_page.py
│   └── elements_page.py
├── utils/
│   ├── driver_factory.py
│   └── helpers.py
├── config/
│   └── config.yaml
├── reports/
│   └── test_report.html
└── conftest.py
    
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown (e.g., driver_factory.py).
  • Page Objects: Encapsulate page elements and actions (e.g., elements_page.py with methods to find multiple elements).
  • Tests: Test scripts that use page objects to perform actions and assertions (e.g., test_elements.py).
  • Utilities: Helper functions for common tasks like waits or data handling (helpers.py).
  • Configuration: Environment settings, URLs, credentials stored in config.yaml.
Configuration Patterns

Use a YAML file (config/config.yaml) to store environment URLs, browser types, and credentials. Load this config in conftest.py to provide fixtures for tests.

# Example config.yaml
base_url: "https://example.com"
browser: "chrome"
credentials:
  username: "testuser"
  password: "password123"
    

Use fixtures in conftest.py to initialize WebDriver based on config and clean up after tests.

Test Reporting and CI/CD Integration
  • Use pytest with pytest-html plugin to generate HTML reports saved in reports/.
  • Integrate tests in CI pipelines (e.g., GitHub Actions) to run on every push or pull request.
  • Failing tests highlight issues with element finding or page behavior.
Best Practices
  • Use Page Object Model: Keep element locators and methods in page classes to avoid duplication.
  • Use explicit waits: Wait for elements to be present before finding multiple elements to avoid flaky tests.
  • Use descriptive locators: Prefer CSS selectors or XPath that clearly identify groups of elements.
  • Return lists of WebElements: Methods finding multiple elements should return lists for easy iteration and assertions.
  • Keep tests simple: Tests should call page methods and assert expected counts or properties of multiple elements.
Self Check

Where in this folder structure would you add a new method to find all buttons on a page?

Key Result
Organize Selenium Python tests using Page Object Model with clear layers for driver, pages, tests, utilities, and config to find multiple elements reliably.