Bird
Raised Fist0
Selenium Pythontesting~20 mins

Find element by link text in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Link Text Locator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ locator
intermediate
2:00remaining
Locate element using exact link text
Which Selenium Python code snippet correctly finds a link with the exact text "Home Page"?
Adriver.find_element(By.LINK_TEXT, "Home Page")
Bdriver.find_element(By.PARTIAL_LINK_TEXT, "Home Page")
Cdriver.find_element(By.ID, "Home Page")
Ddriver.find_element(By.CLASS_NAME, "Home Page")
Attempts:
2 left
💡 Hint
Use the locator that matches the full visible text of the link.
❓ Predict Output
intermediate
2:00remaining
Output of finding link by partial text
What will be the output of this Selenium Python code if the page contains a link with text "Contact Us Today"?
Selenium Python
element = driver.find_element(By.PARTIAL_LINK_TEXT, "Contact")
print(element.text)
A"Us Today"
B"Contact"
C"Contact Us Today"
DNoSuchElementException
Attempts:
2 left
💡 Hint
Partial link text matches any part of the link's visible text.
❓ assertion
advanced
2:00remaining
Valid assertion for link text presence
Which assertion correctly verifies that a link with text "About Us" is present on the page using Selenium Python?
Selenium Python
element = driver.find_element(By.LINK_TEXT, "About Us")
Aassert element.text == "About Us"
Bassert element.get_attribute("href") == "About Us"
Cassert element.is_displayed() == False
Dassert element.text != "About Us"
Attempts:
2 left
💡 Hint
Check the visible text of the found element.
🔧 Debug
advanced
2:00remaining
Identify error in link text locator code
What error will this Selenium Python code raise?
driver.find_element(By.LINKTEXT, "Services")
Selenium Python
driver.find_element(By.LINKTEXT, "Services")
ASyntaxError
BNoSuchElementException
CTypeError
DAttributeError: module 'selenium.webdriver.common.by' has no attribute 'LINKTEXT'
Attempts:
2 left
💡 Hint
Check the spelling of the locator constant.
❓ framework
expert
2:00remaining
Best practice for waiting for link by text
In Selenium Python, which code snippet correctly waits up to 10 seconds for a link with text "Login" to be clickable before clicking it?
Adriver.find_element(By.LINK_TEXT, "Login").click()
BWebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Login"))).click()
C
time.sleep(10)
driver.find_element(By.LINK_TEXT, "Login").click()
DWebDriverWait(driver, 10).until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Login"))).click()
Attempts:
2 left
💡 Hint
Use explicit waits with expected conditions for clickable elements.

Practice

(1/5)
1. What does the Selenium method find_element_by_link_text do?
easy
A. It finds elements by their CSS class name.
B. It finds any element by its HTML tag name.
C. It finds a link on the page by its exact visible text.
D. It finds elements by their XPath expression.

Solution

  1. Step 1: Understand the method purpose

    find_element_by_link_text specifically searches for anchor (<a>) elements by their visible text on the page.
  2. Step 2: Compare with other locator types

    Other methods like finding by tag name, class name, or XPath do not focus on link text but on different attributes or structures.
  3. Final Answer:

    It finds a link on the page by its exact visible text. -> Option C
  4. Quick Check:

    find_element_by_link_text = exact link text match [OK]
Hint: Link text locator matches exact visible text only [OK]
Common Mistakes:
  • Confusing link text with partial text
  • Using it to find non-link elements
  • Assuming it matches partial text automatically
2. Which of the following is the correct syntax to find a link with text "Home" using Selenium in Python?
easy
A. driver.find_element_link_text("Home")
B. driver.find_element_by_link("Home")
C. driver.find_element_by_text_link("Home")
D. driver.find_element_by_link_text("Home")

Solution

  1. Step 1: Recall Selenium method naming

    The correct method to find a link by its exact text is find_element_by_link_text.
  2. Step 2: Check method call syntax

    The method is called on the driver object with the link text as a string argument, e.g., driver.find_element_by_link_text("Home").
  3. Final Answer:

    driver.find_element_by_link_text("Home") -> Option D
  4. Quick Check:

    Correct method name and syntax = driver.find_element_by_link_text("Home") [OK]
Hint: Remember full method name: find_element_by_link_text [OK]
Common Mistakes:
  • Using incomplete or incorrect method names
  • Missing parentheses or quotes
  • Confusing with other locator methods
3. What will be the output of the following code snippet if the page contains a link with text exactly "Contact Us"?
element = driver.find_element_by_link_text("Contact Us")
print(element.tag_name)
medium
A. a
B. div
C. span
D. NoSuchElementException

Solution

  1. Step 1: Understand what find_element_by_link_text returns

    This method returns the anchor (<a>) element whose visible text matches exactly "Contact Us".
  2. Step 2: Check the tag name of a link element

    Links in HTML are represented by the <a> tag, so element.tag_name will be "a".
  3. Final Answer:

    a -> Option A
  4. Quick Check:

    Link tag name = "a" [OK]
Hint: Link elements always have tag name 'a' [OK]
Common Mistakes:
  • Assuming tag name is 'div' or 'span'
  • Expecting text output instead of tag name
  • Not handling exceptions if link missing
4. Given the code below, what is the error and how to fix it?
element = driver.find_element_by_link_text('About')
print(element.text)

Assume the link text on the page is "About Us".
medium
A. SyntaxError due to missing parentheses; fix by adding parentheses.
B. NoSuchElementException because link text does not match exactly; fix by using full text "About Us".
C. AttributeError because element has no text attribute; fix by using element.get_text().
D. No error; code works fine.

Solution

  1. Step 1: Check link text matching requirement

    The method find_element_by_link_text requires exact visible text match. "About" does not match "About Us" exactly.
  2. Step 2: Identify the error and fix

    Because of mismatch, Selenium raises NoSuchElementException. Fix is to use the exact link text: 'About Us'.
  3. Final Answer:

    NoSuchElementException because link text does not match exactly; fix by using full text "About Us". -> Option B
  4. Quick Check:

    Exact text match needed = NoSuchElementException because link text does not match exactly; fix by using full text "About Us". [OK]
Hint: Link text must match exactly, no partial matches [OK]
Common Mistakes:
  • Using partial link text with find_element_by_link_text
  • Confusing element.text with get_text()
  • Ignoring exception when element not found
5. You want to click a link that has the visible text "Learn More" but sometimes the link text changes to "Learn More About Us". How can you reliably find and click this link using Selenium in Python?
hard
A. Use find_element_by_partial_link_text("Learn More") to match both variations.
B. Use find_element_by_link_text("Learn More") only and ignore other cases.
C. Use find_element_by_id("learnMoreLink") assuming the link has an id.
D. Use find_element_by_tag_name("a") and click the first link.

Solution

  1. Step 1: Understand the problem with exact link text

    Since the link text changes between "Learn More" and "Learn More About Us", exact match with find_element_by_link_text will fail for one variation.
  2. Step 2: Use partial link text locator

    find_element_by_partial_link_text("Learn More") matches any link containing "Learn More", covering both cases reliably.
  3. Step 3: Consider other options

    Using id requires knowing the id attribute, which may not exist. Clicking the first <a> tag is unreliable and may click wrong link.
  4. Final Answer:

    Use find_element_by_partial_link_text("Learn More") to match both variations. -> Option A
  5. Quick Check:

    Partial link text matches multiple variations [OK]
Hint: Partial link text finds links with text containing given string [OK]
Common Mistakes:
  • Using exact link text when text varies
  • Assuming link has id without checking
  • Clicking first link without filtering
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