0
0
Selenium-pythonHow-ToBeginner · 3 min read

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's name attribute.
  • "element_name": The exact value of the name attribute 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.ID instead of By.NAME).
  • Typos in the name attribute value.
  • Trying to find elements before the page or element has loaded.
  • Assuming find_element returns 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 TypeUsageDescription
By.NAMEdriver.find_element(By.NAME, "value")Finds element by its name attribute
By.IDdriver.find_element(By.ID, "value")Finds element by its id attribute
By.CLASS_NAMEdriver.find_element(By.CLASS_NAME, "value")Finds element by its class attribute
By.CSS_SELECTORdriver.find_element(By.CSS_SELECTOR, "selector")Finds element using CSS selector
By.XPATHdriver.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.