How to Find Element by Name in Selenium: Simple Guide
In Selenium, you can find an element by its
name attribute using driver.find_element(By.NAME, "element_name"). This method locates the first element matching the given name on the web page.Syntax
The syntax to find an element by name in Selenium uses the find_element method with the By.NAME locator.
driver: Your Selenium WebDriver instance controlling the browser.find_element: Method to locate a single element.By.NAME: Locator strategy specifying to search by the element'snameattribute."element_name": The exact value of thenameattribute you want to find.
python
element = driver.find_element(By.NAME, "element_name")Example
This example opens a browser, navigates to a sample page, and finds an input element by its name attribute. It then types text into that input.
python
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options import time # Setup Chrome WebDriver (adjust path to your chromedriver) options = Options() options.add_argument('--headless') # Run browser in headless mode service = Service(executable_path='chromedriver') driver = webdriver.Chrome(service=service, options=options) try: driver.get('https://www.w3schools.com/html/html_forms.asp') # Find the input element by name attribute 'firstname' input_element = driver.find_element(By.NAME, 'firstname') input_element.clear() input_element.send_keys('Selenium Test') print('Input element found and text entered successfully.') finally: driver.quit()
Output
Input element found and text entered successfully.
Common Pitfalls
Common mistakes when finding elements by name include:
- Using the wrong locator type (e.g.,
By.IDinstead ofBy.NAME). - Typos in the
nameattribute value. - Trying to find elements before the page or element has loaded.
- Assuming
find_elementreturns multiple elements (it returns only the first match).
Always ensure the element is present and visible before interacting.
python
from selenium.webdriver.common.by import By # Wrong way: Using By.ID instead of By.NAME # element = driver.find_element(By.ID, 'firstname') # This will fail if 'firstname' is a name, not an id # Right way: element = driver.find_element(By.NAME, 'firstname')
Quick Reference
| Locator Type | Usage | Description |
|---|---|---|
| By.NAME | driver.find_element(By.NAME, "value") | Finds element by its name attribute |
| By.ID | driver.find_element(By.ID, "value") | Finds element by its id attribute |
| By.CLASS_NAME | driver.find_element(By.CLASS_NAME, "value") | Finds element by its class attribute |
| By.CSS_SELECTOR | driver.find_element(By.CSS_SELECTOR, "selector") | Finds element using CSS selector |
| By.XPATH | driver.find_element(By.XPATH, "xpath") | Finds element using XPath expression |
Key Takeaways
Use driver.find_element(By.NAME, "element_name") to locate elements by their name attribute.
Ensure the name attribute value matches exactly and the element is loaded before searching.
find_element returns the first matching element; use find_elements for multiple matches.
Avoid mixing locator types; use By.NAME only when targeting the name attribute.
Use explicit waits if elements load dynamically to avoid errors.