0
0
Selenium Javatesting~5 mins

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

Choose your learning style9 modes available
Recall & Review
beginner
What does the findElement(By.name()) method do in Selenium?
It locates the first web element on the page that has the specified name attribute value.
Click to reveal answer
beginner
How do you write a Selenium Java command to find an input field with the name "username"?
Use driver.findElement(By.name("username")) to locate the input field by its name attribute.
Click to reveal answer
intermediate
Why is using name attribute a good locator strategy in Selenium?
Because name attributes are often unique for form elements and stable, making tests less fragile.
Click to reveal answer
intermediate
What happens if findElement(By.name()) does not find any matching element?
Selenium throws a NoSuchElementException, causing the test to fail unless handled.
Click to reveal answer
beginner
Write a simple Selenium Java snippet to enter text "hello" into a field found by name "search".
WebElement searchBox = driver.findElement(By.name("search")); searchBox.sendKeys("hello");
Click to reveal answer
Which Selenium method finds the first element with a specific name attribute?
AfindElement(By.name())
BfindElement(By.id())
CfindElement(By.className())
DfindElement(By.tagName())
What exception is thrown if findElement(By.name()) finds no element?
ATimeoutException
BElementNotVisibleException
CNoSuchElementException
DStaleElementReferenceException
Which is a good reason to use name attribute for locating elements?
AIt changes frequently with page reloads
BIt is often unique and stable for form inputs
CIt is always the same as the element's text
DIt is the only locator Selenium supports
How do you enter text into an element found by name "email"?
Adriver.findElement(By.name("email")).sendKeys("text")
Bdriver.findElement(By.id("email")).click()
Cdriver.findElement(By.name("email")).clear()
Ddriver.findElement(By.className("email")).submit()
If multiple elements share the same name, what does findElement(By.name()) return?
AA list of elements
BAll matching elements
CAn error
DThe first matching element
Explain how to use findElement(By.name()) in Selenium Java to locate and interact with a web element.
Think about how you find a form field by its name and type into it.
You got /4 concepts.
    What are the advantages and possible pitfalls of using the name attribute as a locator in Selenium tests?
    Consider reliability and exceptions when element is missing.
    You got /4 concepts.