October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Use the Name Locator in Selenium

Selenium’s name locator matches an element’s exact name attribute. See examples for Python, Java, and JavaScript, plus ways to handle duplicates and failed lookups.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Selenium’s name locator to find an element by the exact value of its HTML name attribute. In Python, write driver.find_element(By.NAME, "newsletter"); in Java or JavaScript, use driver.findElement(By.name("newsletter")). The value is not the control’s visible label or text.

Find an element by name

In this Python example, replace the page URL and newsletter with the page and attribute value used by your test:

As an Amazon Associate I earn from qualifying purchases.

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://example.com/form")

newsletter = driver.find_element(By.NAME, "newsletter")

By.NAME tells Selenium to match the element’s name attribute. For example, it can locate markup such as <input name="newsletter">. It does not search for the word “newsletter” in the element’s label, visible text, ID, or value.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Java

WebElement newsletter = driver.findElement(By.name("newsletter"));

JavaScript

const newsletter = await driver.findElement(By.name('newsletter'));

The Selenium documentation describes eight traditional locator strategies: class name, CSS selector, ID, name, link text, partial link text, tag name, and XPath. A name locator is a direct choice when the page has a useful name attribute.

Handle duplicate name values

A page can contain more than one element with the same name, even though Selenium’s locator guidance says name values should generally be unique. A singular lookup with find_element returns the first match in the search context; it does not guarantee that the match is the control you intended.

Use find_elements when duplicates are possible. It returns the matching elements as a collection, or an empty list if there are no matches:

matches = driver.find_elements(By.NAME, "newsletter")

if not matches:
    raise LookupError("No element has name='newsletter'")

for element in matches:
    print(element.tag_name, element.get_attribute("type"), element.get_attribute("value"))

If the page has repeated names in separate sections, first locate a meaningful parent and search within it:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
form = driver.find_element(By.ID, "signup-form")
newsletter = form.find_element(By.NAME, "newsletter")

Use a parent locator only when that parent is itself a reliable way to identify the intended section.

Choose the locator that best identifies the control

  • ID: Selenium generally prefers an ID when it is available, unique, and predictable.
  • Name: Use it when the page’s name attribute clearly identifies the control you need.
  • CSS selector: Choose CSS when you need to match an attribute together with other conditions, such as a particular element type.

There is no universally best locator. Prefer one that is unique enough for the intended element, stable as the page changes, and clear to someone maintaining the test.

Troubleshoot a name lookup

  • No element found: Check the exact spelling and capitalization of the name attribute in the page markup. Confirm that your test has navigated to the expected page before locating the element.
  • The wrong element was returned: Check whether the page has duplicate name values. Use find_elements to inspect the matches, or narrow the search to a meaningful parent.
  • You searched for a label or visible text: A name locator matches the name attribute, not the control’s label or displayed text. Inspect the markup and use a locator strategy suited to the attribute or content you actually need.
  • The markup has no useful name: Use another locator, such as a stable unique ID or an appropriate CSS selector, rather than assuming the visible label is a name attribute.

Or skip the browser setup

If your goal is a clean screenshot rather than locating or interacting with a form control in a Selenium test, ScreenshotNeo can capture a page with one GET request. This is a separate option: it does not replace Selenium’s DOM locators.

ScreenshotNeo API documentation

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo accepts cookie or consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed. Its MCP server provides screenshot tools for AI agents, including Claude, Cursor, and other MCP clients. The free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Sign up for 1,000 free screenshots a month, with no card required.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

Does Selenium’s name locator match the element’s value?

No. It matches the element’s `name` attribute. The `value` attribute is separate.

Can a name locator find a checkbox?

Yes, if the checkbox has the name attribute value you specify. You can inspect its tag, type, and value after locating it.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.