0
0
Selenium Javatesting~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LinkText Locator 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 its exact visible text and clicks it. What will happen when this code runs if the link with text "Home" exists on the page?
Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
WebElement link = driver.findElement(By.linkText("Home"));
link.click();
System.out.println("Clicked the Home link");
AThe browser navigates to the Home page and prints "Clicked the Home link"
BNoSuchElementException is thrown because linkText is case-insensitive
CStaleElementReferenceException is thrown immediately after findElement
DThe code compiles but does nothing because linkText only works with partial text
Attempts:
2 left
💡 Hint
Remember that findElement(By.linkText()) matches the exact visible text of the link.
assertion
intermediate
2:00remaining
Which assertion correctly verifies the link text found by findElement(By.linkText())?
You have located a link using findElement(By.linkText("Contact Us")). Which assertion correctly checks that the link's visible text is exactly "Contact Us"?
Selenium Java
WebElement contactLink = driver.findElement(By.linkText("Contact Us"));
AassertTrue(contactLink.getText().contains("Contact"));
BassertNotNull(contactLink.getAttribute("href"));
CassertFalse(contactLink.getText().equalsIgnoreCase("contact us"));
DassertEquals("Contact Us", contactLink.getText());
Attempts:
2 left
💡 Hint
Check for exact text equality, not partial or case-insensitive matches.
🔧 Debug
advanced
2:00remaining
Why does this findElement(By.linkText()) call throw NoSuchElementException?
Given the code below, why does the findElement call throw NoSuchElementException even though the link text "About Us" is visible on the page?
Selenium Java
WebElement aboutLink = driver.findElement(By.linkText("About Us"));
AThe linkText locator only works for partial matches, so exact text fails
BThe linkText locator is case-insensitive and the text "About Us" does not match exactly
CThe element is inside an iframe and driver is not switched to it
DThe link text contains extra spaces or hidden characters not visible to the user
Attempts:
2 left
💡 Hint
Check if the element is inside a frame or iframe that requires switching context.
🧠 Conceptual
advanced
2:00remaining
What is a limitation of using findElement(By.linkText()) in Selenium tests?
Which of the following is a known limitation when using findElement(By.linkText()) to locate links in Selenium?
AIt can locate elements inside shadow DOM without extra handling
BIt only matches links with exact visible text, so partial matches fail
CIt ignores case differences in the link text
DIt automatically waits for the element to appear up to 30 seconds
Attempts:
2 left
💡 Hint
Think about how exact the text match must be for linkText locator.
framework
expert
3:00remaining
How to implement a robust method to click a link by visible text using Selenium Java?
You want to create a reusable method in your Selenium Java test framework that clicks a link by its visible text. The method should handle the case where the link is inside an iframe and wait up to 10 seconds for the link to be clickable. Which code snippet correctly implements this?
A
public void clickLinkByText(WebDriver driver, String text) {
  driver.switchTo().frame("frame1");
  new WebDriverWait(driver, Duration.ofSeconds(10))
    .until(ExpectedConditions.elementToBeClickable(By.linkText(text))).click();
  driver.switchTo().defaultContent();
}
B
public void clickLinkByText(WebDriver driver, String text) {
  new WebDriverWait(driver, Duration.ofSeconds(10))
    .until(ExpectedConditions.elementToBeClickable(By.linkText(text))).click();
}
C
public void clickLinkByText(WebDriver driver, String text) {
  driver.switchTo().defaultContent();
  driver.findElement(By.linkText(text)).click();
}
D
public void clickLinkByText(WebDriver driver, String text) {
  driver.switchTo().frame(0);
  driver.findElement(By.partialLinkText(text)).click();
  driver.switchTo().parentFrame();
}
Attempts:
2 left
💡 Hint
Consider iframe switching, explicit wait, and exact link text matching.