0
0
Selenium Pythontesting~15 mins

Find element by partial link text in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify navigation by clicking a link using partial link text
Preconditions (2)
Step 1: Locate the link using partial link text 'Selenium'
Step 2: Click on the located link
Step 3: Wait for the new page to load
Step 4: Verify that the current URL contains 'selenium-automation'
✅ Expected Result: The user is navigated to the Selenium Automation page and the URL contains 'selenium-automation'
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the link is found using partial link text
Verify the click action navigates to the correct URL containing 'selenium-automation'
Best Practices:
Use explicit waits to wait for elements and page load
Use By.PARTIAL_LINK_TEXT locator strategy
Handle exceptions if element is not found
Keep code readable and maintainable
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

# Setup WebDriver (Assuming chromedriver is in PATH)
driver = webdriver.Chrome()

try:
    # Open homepage
    driver.get('https://example.com')

    # Wait until link with partial text 'Selenium' is present
    wait = WebDriverWait(driver, 10)
    link = wait.until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, 'Selenium')))

    # Click the link
    link.click()

    # Wait until URL contains 'selenium-automation'
    wait.until(EC.url_contains('selenium-automation'))

    # Assertion: Check current URL
    current_url = driver.current_url
    assert 'selenium-automation' in current_url, f"URL does not contain 'selenium-automation', got {current_url}"

finally:
    driver.quit()

This script uses Selenium WebDriver with Python to automate the manual test case.

First, it opens the homepage URL.

Then it waits explicitly for the link containing the partial text 'Selenium' to be clickable. This ensures the element is ready for interaction.

After clicking the link, it waits until the URL contains the expected substring 'selenium-automation' to confirm navigation.

Finally, it asserts that the current URL contains the expected text, which verifies the navigation succeeded.

The try-finally block ensures the browser closes even if an error occurs.

Common Mistakes - 3 Pitfalls
Using time.sleep() instead of explicit waits
{'mistake': 'Using By.LINK_TEXT instead of By.PARTIAL_LINK_TEXT when partial text is needed', 'why_bad': "By.LINK_TEXT requires exact match, so the element won't be found if only partial text is provided.", 'correct_approach': 'Use By.PARTIAL_LINK_TEXT to locate links by a substring of their text.'}
Not handling exceptions when element is not found
Bonus Challenge

Now add data-driven testing with 3 different partial link texts to verify navigation for each.

Show Hint