Contents
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.
Examples
1. There is only a 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.
We shall load the following HTML webpage present at the URL: https://pythonexamples.org/tmp/selenium/index-41.html.
<html>
<body>
<p>Hello World!</p>
<div name="xyz">Read More</div>
<div>Go Back</div>
</body>
</html>
Copy Python Program (Selenium)
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-41.html')
# Find element by id
my_div = driver.find_element(By.NAME, 'xyz')
print(my_div.get_attribute('outerHTML'))
# Close the driver
driver.quit()
Output
<div name="xyz">Read More</div>
2. There are 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.
HTML webpage
We shall load the following webpage present at URL: https://pythonexamples.org/tmp/selenium/index-42.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>
Copy Python Program (Selenium)
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-42.html')
# Find element by id
my_div = driver.find_element(By.NAME, 'xyz')
print(my_div.get_attribute('outerHTML'))
# Close the driver
driver.quit()
Output
<div name="xyz">Read More 1</div>
3. There is 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="apple". When we try to find element by the name='apple' using find_element() function, the function throws NoSuchElementException.
HTML webpage
We shall load the following webpage present at URL: https://pythonexamples.org/tmp/selenium/index-42.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>
Copy Python Program (Selenium)
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-42.html')
# Find element by id
my_div = driver.find_element(By.NAME, 'apple')
print(my_div.get_attribute('outerHTML'))
# Close the driver
driver.quit()
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.