findElement by partialLinkText in Selenium Java - Build an Automation Script
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; import static org.junit.jupiter.api.Assertions.assertTrue; public class PartialLinkTextTest { private WebDriver driver; private WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.manage().window().maximize(); driver.get("https://example.com/homepage"); } @Test public void testNavigateUsingPartialLinkText() { // Locate link by partial link text 'Selenium' WebElement link = wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Selenium"))); link.click(); // Wait for URL to contain 'selenium-automation' wait.until(ExpectedConditions.urlContains("selenium-automation")); // Assert URL contains expected text String currentUrl = driver.getCurrentUrl(); assertTrue(currentUrl.contains("selenium-automation"), "URL should contain 'selenium-automation'"); // Assert page title contains expected text String title = driver.getTitle(); assertTrue(title.contains("Selenium Automation"), "Title should contain 'Selenium Automation'"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
This test uses Selenium WebDriver with JUnit 5 to automate the manual test case.
Setup: We open Chrome browser and navigate to the homepage URL.
Test: We locate the link using By.partialLinkText("Selenium") which finds any link containing the word 'Selenium'. We wait until the link is clickable, then click it.
We then wait explicitly for the URL to contain 'selenium-automation' to ensure the page has loaded.
Assertions check that the current URL and page title contain the expected text, confirming correct navigation.
Teardown: The browser closes after the test to clean up.
This approach uses explicit waits to avoid timing issues and follows best practices for maintainability and reliability.
Now add data-driven testing with 3 different partial link texts to verify navigation for each.
Practice
findElement(By.partialLinkText("Login")) do?Solution
Step 1: Understand the method purpose
The methodfindElement(By.partialLinkText("Login"))searches for a link element whose visible text contains the substring "Login".Step 2: Identify the matching behavior
It returns the first matching link element, not all links or buttons, and does not look for attributes named partialLinkText.Final Answer:
Finds the first link containing the text "Login" anywhere in its visible text. -> Option BQuick Check:
Partial link text finds first matching link [OK]
- Confusing partialLinkText with exact link text
- Expecting multiple elements returned
- Using it to find buttons or other elements
- Assuming it matches attributes instead of visible text
Solution
Step 1: Recall correct Selenium locator syntax
The correct method to find a link by partial text isBy.partialLinkText("text").Step 2: Check each option's method name
Only driver.findElement(By.partialLinkText("Register")); uses the correct method namepartialLinkText. Others are invalid or do not exist in Selenium.Final Answer:
driver.findElement(By.partialLinkText("Register")); -> Option DQuick Check:
Correct method name is partialLinkText [OK]
- Using incorrect method names like partialLink or partialTextLink
- Confusing partialLinkText with linkTextContains
- Missing parentheses or quotes in syntax
- Using By.linkText for partial matches
<a href='home.html'>Home Page</a>
<a href='login.html'>User Login</a>
<a href='logout.html'>Logout</a>What will the following Java Selenium code return?
WebElement link = driver.findElement(By.partialLinkText("Log"));
String href = link.getAttribute("href");Solution
Step 1: Identify links containing "Log"
Links with visible text "User Login" and "Logout" both contain "Log" substring.Step 2: Understand findElement behavior
findElement returns the first matching element in the page order, which is "User Login" with href "login.html".Final Answer:
"login.html" -> Option AQuick Check:
First partial link match is "User Login" [OK]
- Assuming it returns all matches, not first
- Choosing "logout.html" because it also contains "Log"
- Expecting an exception if multiple matches exist
- Confusing visible text with href attribute
WebElement link = driver.findElement(By.partialLinkText("Sign Up"));But it throws
NoSuchElementException even though the page has a link with text "SignUp Now".What is the most likely cause?
Solution
Step 1: Check partialLinkText matching rules
partialLinkText requires an exact substring match in the link's visible text, case-sensitive and including spaces. "Sign Up" (with space after "Sign") is not a substring of "SignUp Now" (no space).Step 2: Identify the most likely cause
The spacing mismatch causes NoSuchElementException. Iframe issues are possible but less likely given the highlighted text difference.Final Answer:
The partialLinkText "Sign Up" does not match because of the missing space in "SignUp". -> Option CQuick Check:
partialLinkText requires exact substring match incl. spaces [OK]
- Thinking partialLinkText ignores spaces or whitespace
- Using linkText instead of partialLinkText unnecessarily
- Not verifying exact visible link text on page
- Overlooking iframe context unnecessarily
How can you ensure Selenium clicks the "Edit Profile" link using
findElement(By.partialLinkText())?Solution
Step 1: Understand broad partialLinkText limitation
By.partialLinkText("Profile")finds the first link like "User Profile", not necessarily "Edit Profile".Step 2: Use precise substring with findElement
driver.findElement(By.partialLinkText("Edit Profile"))targets links containing that specific phrase, matching "Edit Profile" without assuming order or using findElements.Final Answer:
Usedriver.findElement(By.partialLinkText("Edit Profile"))to match exactly that link. -> Option AQuick Check:
Specific substring in partialLinkText ensures target [OK]
- Using vague "Profile" assuming findElement picks desired link
- Relying on findElements and assuming fixed page order
- Ignoring zero-based indexing in element lists
- Switching to linkText ignoring partialLinkText requirement
Ready to prove it?
Pass the test and this pattern moves unknown
