0
0
Selenium Pythontesting~8 mins

Find element by name in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Find element by name
Folder Structure
selenium_python_project/
├── tests/
│   ├── test_login.py
│   └── test_search.py
├── pages/
│   ├── base_page.py
│   └── login_page.py
├── utils/
│   ├── driver_factory.py
│   └── config_reader.py
├── config/
│   ├── config.yaml
│   └── credentials.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., login_page.py uses find_element(By.NAME, "username") to locate elements).
  • Tests: Test scripts that use page objects to perform actions and assertions (e.g., test_login.py).
  • Utilities: Helper functions and config readers (e.g., config_reader.py).
  • Configuration: Environment settings, credentials, and browser options stored in YAML files.
  • Reports: Store test execution reports for review.
Configuration Patterns

Use YAML files in config/ to store environment URLs, browser types, and user credentials.

Example config.yaml:

environment:
  url: "https://example.com"
browser: "chrome"
    

Example credentials.yaml:

user:
  username: "testuser"
  password: "password123"
    

Use config_reader.py to load these settings and pass them to tests and driver setup.

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 each code push.
  • Reports provide pass/fail status and screenshots on failure for easy debugging.
Best Practices
  • Use Page Object Model: Keep element locators and actions in page classes for easy maintenance.
  • Use find_element(By.NAME, "name"): Use Selenium's recommended locator strategy with By for clarity and future-proofing.
  • Explicit Waits: Wait for elements by name to be visible or clickable before interacting to avoid flaky tests.
  • Separate Config from Code: Store URLs, credentials, and browser settings outside code for flexibility.
  • Clear Folder Structure: Organize tests, pages, utils, and configs logically for easy navigation.
Self Check

Where would you add a new page object class for a page that has elements located by their name attribute?

Key Result
Use Page Object Model with Selenium's By.NAME locator and clear folder structure for maintainable test automation.