Find element by partial link text in Selenium Python - Build an Automation Script
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.
Now add data-driven testing with 3 different partial link texts to verify navigation for each.
Practice
find_element_by_partial_link_text do?Solution
Step 1: Understand the method purpose
The methodfind_element_by_partial_link_textsearches for<a>elements that contain the given text partially.Step 2: Compare with other locator methods
Unlike exact text match or tag name search, this method matches any link containing the partial text.Final Answer:
Finds a link element containing the specified partial text. -> Option AQuick Check:
Partial link text = Finds links with partial text [OK]
- Confusing partial link text with exact text match
- Using it on non-link elements
- Expecting it to find by attribute instead of visible text
Solution
Step 1: Check method usage and argument format
The method requires a string argument inside parentheses and quotes, like"Login".Step 2: Validate Python syntax
driver.find_element_by_partial_link_text("Login") uses correct parentheses and quotes. Options A, B, and D have syntax errors or wrong brackets.Final Answer:
driver.find_element_by_partial_link_text("Login") -> Option CQuick Check:
Correct method call uses parentheses and quotes [OK]
- Omitting quotes around the string
- Using square or curly brackets instead of parentheses
- Passing variable without quotes
element = driver.find_element_by_partial_link_text("Login")
print(element.text)Solution
Step 1: Understand partial link text matching
The method finds the first link containing "Login" anywhere in its visible text.Step 2: Check what
It returns the full visible text of the found link, which is "Click here to Login".element.textreturnsFinal Answer:
Click here to Login -> Option DQuick Check:
Partial match finds full link text [OK]
- Expecting only the partial text to print
- Assuming it throws exception if partial matches
- Confusing element.text with attribute values
element = driver.find_element_by_partial_link_text(Login) print(element.text)
Solution
Step 1: Identify the error in argument usage
The argument Login is not in quotes, so Python treats it as a variable, causing NameError.Step 2: Correct the argument to a string
Wrap Login in quotes like "Login" to pass a string literal.Final Answer:
NameError because Login is not quoted; fix by adding quotes: "Login" -> Option BQuick Check:
String arguments need quotes [OK]
- Forgetting quotes around string literals
- Assuming partial link text accepts variables without quotes
- Misreading error type as syntax error
Solution
Step 1: Understand partial link text limitation
The method matches only one partial string at a time; it cannot match multiple alternatives in one call.Step 2: Implement fallback logic
Try finding link with "Sign" first; if not found, catch exception and try "Register" next.Final Answer:
Try finding by partial link text "Sign" first, if not found, then try "Register". -> Option AQuick Check:
Partial link text matches one string; use fallback for multiple options [OK]
- Using 'or' inside partial link text string
- Expecting one call to match multiple partial texts
- Using link_text instead of partial_link_text for partial matches
Ready to prove it?
Pass the test and this pattern moves unknown
