How to Find Element by Name in Selenium Python?

Find Element by Name

To find an HTML Element by name attribute using Selenium in Python, call find_element() method and pass By.NAME as the first argument, and the name attribute’s value (of the HTML Element we need to find) as the second argument.

find_element(By.NAME, "name_value")

If there are multiple HTML Elements with the same given value for name attribute, then find_element() returns the first HTML Element of those.

Scenario 1: HTML page with single element by given name

In the following example, we have an HTML page with a div element whose name="xyz". In the Python program, we will find the HTML element whose name attribute has a value of 'xyz', using find_element() method, and print that element to the console.

index.html

<html>
 <body>
  <p>Hello World!</p>
  <div name="xyz">Read More</div>
  <div>Go Back</div>
 </body>
</html>

Python Program (Selenium)

from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By

service = Service(executable_path="/usr/local/bin/chromedriver")
#initialize web driver
with webdriver.Chrome(service=service) as driver:
    #navigate to the url
    driver.get('http://127.0.0.1:5500/localwebsite/index.html')
    #find element by name
    myDiv = driver.find_element(By.NAME, 'xyz')
    print(myDiv)
    print(myDiv.text)

Output

<selenium.webdriver.remote.webelement.WebElement (session="cb460eaa9a8578155577ff2fcb7d2f80", element="c4c81072-c9de-4a2e-b299-b210875ba108")>
Read More

Scenario 2: HTML page with multiple elements by given name

Now, let us consider the scenario where there are multiple HTML Elements with the same name. We will use find_element() function to find the first element whose name attribute has a value of 'xyz'.

Even through there are two elements with the same given name, find_element() returns only the first element.

index.html

<html>
 <body>
  <p>First Article</p>
  <div name="xyz">Read More 1</div>
  <div>Go Back</div>
  <br>
  <p>Second Article</p>
  <div name="xyz">Read More 2</div>
  <div>Go Back</div>
 </body>
</html>

Python Program (Selenium)

from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By

service = Service(executable_path="/usr/local/bin/chromedriver")
#initialize web driver
with webdriver.Chrome(service=service) as driver:
    #navigate to the url
    driver.get('http://127.0.0.1:5500/localwebsite/index.html')
    #find element by name
    myDiv = driver.find_element(By.NAME, 'xyz')
    print(myDiv)
    print(myDiv.text)

Output

<selenium.webdriver.remote.webelement.WebElement (session="b5a43ce51bea10f1438b4dfec969b559", element="465d82ec-4655-4276-bdc7-7fb0dd89572b")>
Read More 1

Scenario 3: HTML page with no element by given name

If there is no element by given name, find_element() function raises NoSuchElementException.

In the following example, we have taken an HTML page with no elements by name="xyz". When we try to find element by the name='xyz' using find_element() function, the function throws NoSuchElementException.

index.html

<html>
 <body>
  <p>First Article</p>
  <div>Read More 1</div>
  <div>Go Back</div>
  <br>
  <p>Second Article</p>
  <div>Read More 2</div>
  <div>Go Back</div>
 </body>
</html>

Python Program (Selenium)

from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By

service = Service(executable_path="/usr/local/bin/chromedriver")
#initialize web driver
with webdriver.Chrome(service=service) as driver:
    #navigate to the url
    driver.get('http://127.0.0.1:5500/localwebsite/index.html')
    #find element by name
    myDiv = driver.find_element(By.NAME, 'xyz')
    print(myDiv)
    print(myDiv.text)

Output

    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="asdf"]"}

Summary

In this tutorial of Python Examples, we learned how to find an element by name attribute in webpage using Selenium.