0
0
Selenium Pythontesting~8 mins

Find element by link text in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Find element by link text
Folder Structure
selenium_project/
├── tests/
│   ├── test_login.py
│   └── test_navigation.py
├── pages/
│   ├── base_page.py
│   └── home_page.py
├── utils/
│   ├── driver_factory.py
│   └── wait_utils.py
├── config/
│   └── config.yaml
├── conftest.py
└── requirements.txt
    

This structure separates tests, page objects, utilities, and configuration files clearly.

Test Framework Layers
  • Driver Layer: utils/driver_factory.py creates and manages WebDriver instances.
  • Page Objects: pages/ contains classes representing web pages. Each class has methods to find elements and perform actions. For example, home_page.py uses find_element(By.LINK_TEXT, "link text") to locate links.
  • Tests: tests/ contains test scripts that use page objects to perform actions and assertions.
  • Utilities: Helper functions like explicit waits in utils/wait_utils.py to improve test stability.
  • Configuration: config/config.yaml stores environment URLs, browser types, and credentials.
Configuration Patterns

Use a YAML file (config/config.yaml) to store settings:

environment:
  base_url: "https://example.com"
  browser: "chrome"
  implicit_wait: 10
credentials:
  username: "testuser"
  password: "password123"
    

Load these settings in conftest.py and pass them to tests and page objects. This allows easy switching between environments and browsers without changing code.

Test Reporting and CI/CD Integration
  • Use pytest as the test runner with built-in reporting.
  • Integrate pytest-html plugin to generate readable HTML reports after test runs.
  • Configure CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests on each code push and publish reports.
  • Use screenshots on failure saved in a reports/screenshots/ folder for easier debugging.
Best Practices
  • Use Page Object Model: Encapsulate element locators and actions in page classes to keep tests clean.
  • Use Explicit Waits: Wait for elements like links to be clickable before interacting to avoid flaky tests.
  • Use Descriptive Link Text: When using find_element(By.LINK_TEXT, "link text"), ensure the link text is unique and stable.
  • Keep Locators Maintainable: Avoid hardcoding link texts in tests; keep them in page objects.
  • Separate Config from Code: Store URLs, credentials, and browser settings outside code for flexibility.
Self Check

Where in this folder structure would you add a new method to find a link by its text on the Home page?

Key Result
Organize Selenium Python tests using Page Object Model with clear folder layers and config for maintainability.