0
0
Selenium Pythontesting~8 mins

Find element by partial link text in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Find element by partial link text
Folder Structure
selenium_project/
├── tests/
│   ├── test_login.py
│   ├── test_navigation.py
│   └── test_partial_link_text.py  <-- tests using partial link text locator
├── pages/
│   ├── base_page.py
│   ├── login_page.py
│   └── home_page.py  <-- page objects with methods using partial link text
├── utils/
│   ├── driver_factory.py
│   └── wait_utils.py
├── config/
│   ├── config.yaml
│   └── credentials.yaml
├── conftest.py  <-- pytest fixtures for setup/teardown
└── requirements.txt
    
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown (e.g., driver_factory.py).
  • Page Objects: Classes representing pages, with methods locating elements by partial link text using Selenium's find_element(By.PARTIAL_LINK_TEXT, "text").
  • Tests: Test scripts in tests/ folder that call page object methods to perform actions and assertions.
  • Utilities: Helper functions for waits, logging, and common actions.
  • Configuration: YAML or JSON files storing environment URLs, browser types, and credentials.
Configuration Patterns

Use config/config.yaml to store environment URLs and browser preferences:

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

Use credentials.yaml for sensitive data, loaded securely in tests.

Use conftest.py to read config and provide fixtures that initialize WebDriver accordingly.

Test Reporting and CI/CD Integration
  • Use pytest with pytest-html plugin to generate readable HTML reports after test runs.
  • Integrate tests into CI pipelines (GitHub Actions, Jenkins) to run on every commit.
  • Reports show which tests passed or failed, including those using partial link text locators.
  • Logs capture screenshots on failure for debugging locator issues.
Best Practices
  • Use By.PARTIAL_LINK_TEXT only when full link text is dynamic or too long.
  • Prefer unique and stable locators; partial link text can be brittle if link texts change.
  • Wrap locator usage in page object methods to isolate locator changes.
  • Use explicit waits to ensure elements located by partial link text are present before interaction.
  • Keep test data and locators separate from test logic for easier maintenance.
Self Check

Where in this folder structure would you add a new page object method that finds a link by partial link text?

Key Result
Use Page Object Model with Selenium's partial link text locator wrapped in page methods for maintainable tests.