0
0
Selenium Pythontesting~8 mins

Find element by ID in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Find element by ID
Folder Structure
selenium_python_project/
├── tests/
│   ├── test_login.py
│   └── test_search.py
├── pages/
│   ├── base_page.py
│   └── login_page.py
├── utils/
│   ├── driver_factory.py
│   └── config.py
├── reports/
│   └── test_report.html
├── conftest.py
└── requirements.txt
  
Test Framework Layers
  • Driver Layer: utils/driver_factory.py manages browser setup and teardown.
  • Page Objects: pages/ contains classes representing web pages, each with methods to find elements by ID and interact with them.
  • Tests: tests/ holds test scripts that use page objects to perform actions and assertions.
  • Utilities: utils/config.py stores configuration like URLs and credentials.
  • Fixtures: conftest.py defines setup and teardown hooks for tests.
Configuration Patterns

Use utils/config.py to store environment variables such as base URLs and credentials. Use conftest.py to read these configurations and pass them to tests and driver setup.

Example: config.py holds a dictionary with environment details. driver_factory.py reads browser choice from environment variables or config file to launch the correct browser.

Test Reporting and CI/CD Integration
  • Use pytest with pytest-html plugin to generate HTML reports saved in reports/.
  • Integrate tests in CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests on code push and publish reports.
  • Use clear test names and assertions to make reports easy to understand.
Best Practices
  1. Use Page Object Model: Encapsulate element locators and actions in page classes to keep tests clean.
  2. Locate Elements by ID: IDs are unique and fast to find, so prefer find_element(By.ID, "element_id") when possible.
  3. Explicit Waits: Use explicit waits to wait for elements by ID to be present before interacting.
  4. Keep Locators Centralized: Store element IDs as constants or variables in page objects for easy maintenance.
  5. Readable Test Names: Name tests clearly to reflect what element by ID is being tested.
Self Check

Where in this folder structure would you add a new page object class for a page that contains an element you want to find by ID?

Key Result
Use Page Object Model with unique element IDs and explicit waits for reliable Selenium Python tests.