Bird
Raised Fist0
Selenium Javatesting~20 mins

findElement by partialLinkText in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PartialLinkText Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium Java code snippet?

Consider the following Selenium Java code that tries to find a link by partial link text and click it. What will be the output if the page contains a link with text "Learn Selenium Testing"?

Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("http://example.com");
WebElement link = driver.findElement(By.partialLinkText("Selenium"));
link.click();
System.out.println("Clicked link with partial text 'Selenium'");
ANoSuchElementException at findElement
BStaleElementReferenceException at click
CTimeoutException waiting for page load
DClicked link with partial text 'Selenium'
Attempts:
2 left
💡 Hint

Partial link text matches any part of the link's visible text.

❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies the link text found by partialLinkText?

You have located a link using driver.findElement(By.partialLinkText("Start")). The full link text is "Start Your Free Trial". Which assertion correctly verifies the link text?

Selenium Java
WebElement link = driver.findElement(By.partialLinkText("Start"));
AassertEquals("Start Your Free Trial", link.getText());
BassertFalse(link.getText().startsWith("Free"));
CassertTrue(link.getText().contains("Start"));
DassertNull(link.getText());
Attempts:
2 left
💡 Hint

Partial link text locator finds links containing the substring, so the text should contain "Start".

🔧 Debug
advanced
2:00remaining
Why does this Selenium code throw NoSuchElementException?

The following code throws NoSuchElementException. The page has a link with text "Contact Us Today". What is the most likely reason?

Selenium Java
WebElement link = driver.findElement(By.partialLinkText("contact us"));
link.click();
AThe link is inside an iframe and not switched to it
BPartial link text is case sensitive, so "contact us" does not match "Contact Us Today"
CThe link is disabled and cannot be found
DThe page has not loaded yet, causing the element to be missing
Attempts:
2 left
💡 Hint

Check if the element is inside an iframe or frame.

❓ framework
advanced
2:00remaining
Which TestNG annotation is best to initialize WebDriver before tests using partialLinkText?

You want to initialize the WebDriver once before all tests that use findElement(By.partialLinkText()). Which TestNG annotation should you use?

A@BeforeClass
B@BeforeMethod
C@BeforeTest
D@BeforeSuite
Attempts:
2 left
💡 Hint

Consider initializing WebDriver once per test class.

🧠 Conceptual
expert
2:00remaining
What is a limitation of using findElement(By.partialLinkText()) in Selenium?

Which of the following is a known limitation when using findElement(By.partialLinkText())?

AIt only works with exact full link text matches
BIt can match multiple links, but returns only the first match, which may cause ambiguity
CIt requires the link text to be an exact case-sensitive match
DIt cannot find links inside shadow DOM elements
Attempts:
2 left
💡 Hint

Think about how partial matching works and what happens if multiple links contain the substring.

Practice

(1/5)
1. What does the Selenium method findElement(By.partialLinkText("Login")) do?
easy
A. Finds the first button with the text "Login".
B. Finds the first link containing the text "Login" anywhere in its visible text.
C. Finds all links that exactly match the text "Login".
D. Finds the first element with the attribute partialLinkText="Login".

Solution

  1. Step 1: Understand the method purpose

    The method findElement(By.partialLinkText("Login")) searches for a link element whose visible text contains the substring "Login".
  2. 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.
  3. Final Answer:

    Finds the first link containing the text "Login" anywhere in its visible text. -> Option B
  4. Quick Check:

    Partial link text finds first matching link [OK]
Hint: PartialLinkText finds first link containing given substring [OK]
Common Mistakes:
  • 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
2. Which of the following is the correct syntax to find a link containing the text "Register" using Selenium in Java?
easy
A. driver.findElement(By.partialTextLink("Register"));
B. driver.findElement(By.partialLink("Register"));
C. driver.findElement(By.linkTextContains("Register"));
D. driver.findElement(By.partialLinkText("Register"));

Solution

  1. Step 1: Recall correct Selenium locator syntax

    The correct method to find a link by partial text is By.partialLinkText("text").
  2. Step 2: Check each option's method name

    Only driver.findElement(By.partialLinkText("Register")); uses the correct method name partialLinkText. Others are invalid or do not exist in Selenium.
  3. Final Answer:

    driver.findElement(By.partialLinkText("Register")); -> Option D
  4. Quick Check:

    Correct method name is partialLinkText [OK]
Hint: Use By.partialLinkText("text") exactly for partial link search [OK]
Common Mistakes:
  • Using incorrect method names like partialLink or partialTextLink
  • Confusing partialLinkText with linkTextContains
  • Missing parentheses or quotes in syntax
  • Using By.linkText for partial matches
3. Given the HTML links:
<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");
medium
A. "login.html"
B. "logout.html"
C. "home.html"
D. NoSuchElementException at runtime

Solution

  1. Step 1: Identify links containing "Log"

    Links with visible text "User Login" and "Logout" both contain "Log" substring.
  2. Step 2: Understand findElement behavior

    findElement returns the first matching element in the page order, which is "User Login" with href "login.html".
  3. Final Answer:

    "login.html" -> Option A
  4. Quick Check:

    First partial link match is "User Login" [OK]
Hint: findElement returns first partial match in page order [OK]
Common Mistakes:
  • 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
4. You wrote this Selenium Java code:
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?
medium
A. You must use By.linkText instead of partialLinkText for multi-word links.
B. partialLinkText cannot find links with multiple words.
C. The partialLinkText "Sign Up" does not match because of the missing space in "SignUp".
D. The link is inside an iframe and not switched to it.

Solution

  1. 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).
  2. Step 2: Identify the most likely cause

    The spacing mismatch causes NoSuchElementException. Iframe issues are possible but less likely given the highlighted text difference.
  3. Final Answer:

    The partialLinkText "Sign Up" does not match because of the missing space in "SignUp". -> Option C
  4. Quick Check:

    partialLinkText requires exact substring match incl. spaces [OK]
Hint: partialLinkText matches exact substrings including spaces [OK]
Common Mistakes:
  • Thinking partialLinkText ignores spaces or whitespace
  • Using linkText instead of partialLinkText unnecessarily
  • Not verifying exact visible link text on page
  • Overlooking iframe context unnecessarily
5. You want to click a link that contains the text "Profile" but the page has multiple links like "User Profile", "Edit Profile", and "Profile Settings".
How can you ensure Selenium clicks the "Edit Profile" link using findElement(By.partialLinkText())?
hard
A. Use driver.findElement(By.partialLinkText("Edit Profile")) to match exactly that link.
B. Use driver.findElement(By.partialLinkText("Profile")) and then filter by href attribute manually.
C. Use driver.findElements(By.partialLinkText("Profile")) and click the second element in the list.
D. Use driver.findElement(By.linkText("Edit Profile")) instead of partialLinkText.

Solution

  1. Step 1: Understand broad partialLinkText limitation

    By.partialLinkText("Profile") finds the first link like "User Profile", not necessarily "Edit Profile".
  2. 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.
  3. Final Answer:

    Use driver.findElement(By.partialLinkText("Edit Profile")) to match exactly that link. -> Option A
  4. Quick Check:

    Specific substring in partialLinkText ensures target [OK]
Hint: Use longer specific substring in partialLinkText for precision [OK]
Common Mistakes:
  • 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard