0
0
Selenium Javatesting~5 mins

findElement by linkText in Selenium Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does findElement(By.linkText()) do in Selenium?
It finds the first web element on the page that exactly matches the visible text of a link (anchor tag).
Click to reveal answer
beginner
How do you use findElement(By.linkText()) to click a link with text "Home"?
Use driver.findElement(By.linkText("Home")).click(); to find and click the link with text "Home".
Click to reveal answer
intermediate
What is the difference between By.linkText() and By.partialLinkText()?
By.linkText() matches the full visible text of a link exactly, while By.partialLinkText() matches any part of the link text.
Click to reveal answer
intermediate
Why should you avoid using findElement(By.linkText()) with very common link texts like "Click here"?
Because it may find the wrong link if multiple links have the same text, causing flaky or incorrect tests.
Click to reveal answer
beginner
What exception is thrown if findElement(By.linkText()) does not find any matching element?
It throws NoSuchElementException indicating no element with the given link text was found.
Click to reveal answer
Which Selenium method finds a link by its exact visible text?
AfindElement(By.partialLinkText())
BfindElement(By.id())
CfindElement(By.className())
DfindElement(By.linkText())
What happens if findElement(By.linkText("Submit")) does not find a matching link?
AThrows NoSuchElementException
BReturns an empty list
CReturns null
DClicks the first link on the page
Which locator should you use to find a link containing the text "Login" anywhere in its visible text?
ABy.linkText("Login")
BBy.id("Login")
CBy.partialLinkText("Login")
DBy.name("Login")
Why is By.linkText() considered a good locator for links?
ABecause link text rarely changes and is visible to users
BBecause it uses element ID
CBecause it matches CSS classes
DBecause it finds hidden elements
Which of these is a valid way to click a link with text "Contact Us" using Selenium Java?
Adriver.findElement(By.id("Contact Us")).click();
Bdriver.findElement(By.linkText("Contact Us")).click();
Cdriver.findElement(By.name("Contact Us")).click();
Ddriver.findElement(By.className("Contact Us")).click();
Explain how findElement(By.linkText()) works and when to use it.
Think about how you find a link on a webpage by reading its text.
You got /4 concepts.
    Describe the difference between By.linkText() and By.partialLinkText() in Selenium.
    Consider exact vs partial matching of link text.
    You got /4 concepts.