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?
✗ Incorrect
The
By.name() locator targets elements by their name attribute.What exception is thrown if
findElement(By.name()) finds no element?✗ Incorrect
If no element matches, Selenium throws
NoSuchElementException.Which is a good reason to use
name attribute for locating elements?✗ Incorrect
name attributes are usually unique and stable, making tests reliable.How do you enter text into an element found by name "email"?
✗ Incorrect
Use
sendKeys() on the element found by By.name("email") to enter text.If multiple elements share the same name, what does
findElement(By.name()) return?✗ Incorrect
findElement returns only the first matching element found.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.