0
0
Selenium Pythontesting~8 mins

Find element by tag name in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Find element by tag name
Folder Structure
selenium_python_project/
├── src/
│   ├── pages/
│   │   └── home_page.py
│   ├── tests/
│   │   └── test_home_page.py
│   ├── utils/
│   │   └── driver_factory.py
│   └── config/
│       └── config.py
├── requirements.txt
└── pytest.ini
    
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown (e.g., driver_factory.py).
  • Page Objects: Encapsulate page elements and actions using locators like tag name (e.g., home_page.py).
  • Tests: Test cases that use page objects to perform actions and assertions (e.g., test_home_page.py).
  • Utilities: Helper functions and reusable code.
  • Configuration: Environment settings, browser options, and credentials (e.g., config.py).
Configuration Patterns

Use a config.py file to store environment URLs, browser choices, and credentials.

Example:

# config/config.py
BASE_URL = "https://example.com"
BROWSER = "chrome"
IMPLICIT_WAIT = 10
    

Use environment variables or command-line options to switch browsers or environments without changing code.

Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html for readable HTML reports.
  • Integrate with CI/CD tools (GitHub Actions, Jenkins) to run tests automatically on code changes.
  • Configure reports to show pass/fail status and screenshots on failure.
Best Practices
  • Use Page Object Model: Keep locators and actions in page classes for easy maintenance.
  • Prefer explicit waits: Wait for elements by tag name to be visible or clickable before interacting.
  • Use meaningful locators: Tag name is simple but often combined with other attributes for precision.
  • Keep tests independent: Each test should set up and clean up its own state.
  • Store config separately: Avoid hardcoding URLs or browser names in test code.
Self Check

Where would you add a new page object class for a page that contains elements you want to find by tag name?

Key Result
Organize Selenium Python tests with clear layers: driver setup, page objects using tag name locators, tests, utilities, and config for maintainability.