We use findElement by name to locate a web page element using its name attribute. This helps us interact with that element in tests.
findElement by name in Selenium Java
WebElement element = driver.findElement(By.name("elementName"));Replace "elementName" with the actual name attribute value of the element.
This returns the first matching element with that name on the page.
username.WebElement usernameField = driver.findElement(By.name("username"));q, common in search forms.WebElement searchBox = driver.findElement(By.name("q"));submit to click or check.WebElement submitButton = driver.findElement(By.name("submit"));This test script opens a login page, finds username and password fields by their name attributes, fills them, and clicks the login button. It prints a success message if all elements are found and clicked.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class FindElementByNameExample { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com/login"); WebElement username = driver.findElement(By.name("username")); username.sendKeys("testuser"); WebElement password = driver.findElement(By.name("password")); password.sendKeys("mypassword"); WebElement loginButton = driver.findElement(By.name("login")); loginButton.click(); System.out.println("Login form submitted successfully."); } catch (Exception e) { System.out.println("Element not found or error occurred."); } finally { driver.quit(); } } }
Always ensure the name attribute is unique or you get the first matching element only.
If the element is not found, Selenium throws a NoSuchElementException.
Use explicit waits if the element loads dynamically to avoid errors.
findElement by name locates elements using their name attribute.
It is useful for form inputs and buttons with unique names.
Remember to handle exceptions if elements are missing.
Practice
findElement(By.name("username")) do in a test script?Solution
Step 1: Understand the method purpose
The methodfindElement(By.name("username"))searches for a single element with thenameattribute equal to "username".Step 2: Differentiate from other locators
This method does not search by tag, id, or class, only by thenameattribute.Final Answer:
It finds the first web element with the attribute name="username". -> Option BQuick Check:
findElement(By.name) = finds element by name attribute [OK]
- Confusing name with id or class attributes
- Thinking it finds multiple elements instead of one
- Assuming it performs a click automatically
Solution
Step 1: Identify the correct locator method
To find an element by its name attribute, the correct method isBy.name("email").Step 2: Verify syntax correctness
The syntaxdriver.findElement(By.name("email"));is valid and correctly uses the name locator.Final Answer:
driver.findElement(By.name("email")); -> Option CQuick Check:
Use By.name for name attribute [OK]
- Using By.id or By.className instead of By.name
- Missing quotes around the name value
- Using incorrect method names
<input name="search">?
WebElement element = driver.findElement(By.name("search"));
System.out.println(element.getTagName());Solution
Step 1: Locate element by name attribute
The code finds the element withname="search", which is aninputtag.Step 2: Print the tag name of the found element
The methodgetTagName()returns the tag name of the element, which is "input".Final Answer:
It prints "input" to the console. -> Option AQuick Check:
getTagName() returns tag name [OK]
- Expecting getTagName() to return attribute value
- Assuming element not found causes print output
- Confusing attribute name with tag name
WebElement element = driver.findElement(By.name("submitBtn"));
element.click();
Assume the page does not have any element with name="submitBtn".Solution
Step 1: Understand findElement behavior when element missing
If no element matches the locator,findElementthrowsNoSuchElementException.Step 2: Check what happens before click()
The exception occurs beforeclick()is called, so click is never reached.Final Answer:
NoSuchElementException will be thrown at findElement. -> Option DQuick Check:
Missing element causes NoSuchElementException [OK]
- Expecting null instead of exception
- Thinking click() causes NullPointerException
- Assuming silent failure without error
Solution
Step 1: Understand iframe context in Selenium
Elements inside iframes require switching the driver's context to that iframe before locating elements inside it.Step 2: Apply correct order of operations
First switch to the iframe, then usefindElement(By.name("userEmail"))to locate the element inside it.Final Answer:
Switch to iframe first, then use driver.findElement(By.name("userEmail")). -> Option AQuick Check:
Switch to iframe before locating inside it [OK]
- Trying to find element inside iframe without switching
- Using wrong locator like id instead of name
- Switching to iframe after locating element
Ready to prove it?
Pass the test and this pattern moves unknown
