Finding elements by their name attribute helps you interact with web page parts easily, like filling forms.
Find element by name in Selenium Python
element = driver.find_element(By.NAME, "element_name")Use By.NAME to specify you are searching by the name attribute.
Replace "element_name" with the actual name value of the element.
username_input = driver.find_element(By.NAME, "username")submit_button = driver.find_element(By.NAME, "submit")checkbox = driver.find_element(By.NAME, "agree_terms")This script opens a simple HTML form in the browser, finds elements by their name attributes, and prints their tag names.
from selenium import webdriver from selenium.webdriver.common.by import By import urllib.parse # Setup WebDriver (make sure the driver executable is in PATH) driver = webdriver.Chrome() # Open a simple test page with a form html = ''' <html> <body> <form> <input type="text" name="username" /> <input type="password" name="password" /> <input type="submit" name="login" value="Login" /> </form> </body> </html> ''' # Encode the HTML content to be used in data URL encoded_html = urllib.parse.quote(html) # Load the HTML content using data URL driver.get("data:text/html;charset=utf-8," + encoded_html) # Find the username input by name username_field = driver.find_element(By.NAME, "username") print(f"Found element with name 'username': tag name is {username_field.tag_name}") # Find the login button by name login_button = driver.find_element(By.NAME, "login") print(f"Found element with name 'login': tag name is {login_button.tag_name}") # Close the browser driver.quit()
Always ensure the element with the given name exists, or Selenium will raise an error.
Use find_elements(By.NAME, "name") to get a list if multiple elements share the same name.
Make sure your WebDriver is set up correctly and matches your browser version.
Use find_element(By.NAME, "name") to locate elements by their name attribute.
This method is useful for interacting with form fields and buttons.
Always handle cases where the element might not be found to avoid errors.
Practice
find_element(By.NAME, "username") do?Solution
Step 1: Understand the method purpose
The methodfind_elementlocates a single element matching the given locator.Step 2: Analyze the locator type
UsingBy.NAMEtargets elements by theirnameattribute, not id or class.Final Answer:
Finds the first element with the name attribute equal to "username". -> Option BQuick Check:
find_element(By.NAME, "username") = first element by name [OK]
- Confusing find_element with find_elements (plural)
- Using id or class instead of name attribute
- Assuming it finds multiple elements
Solution
Step 1: Recall modern Selenium syntax
Since Selenium 4, the correct way is to usefind_element(By.NAME, "email").Step 2: Check each option
driver.find_element_by_name("email") is deprecated syntax, C and D use wrong locator types.Final Answer:
driver.find_element(By.NAME, "email") -> Option CQuick Check:
Use By.NAME with find_element [OK]
- Using deprecated find_element_by_name method
- Mixing locator types like ID or CLASS_NAME
- Forgetting to import By from selenium.webdriver.common.by
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('http://example.com')
element = driver.find_element(By.NAME, 'search')
print(element.tag_name)What will be printed if the page has an input field with
name="search"?Solution
Step 1: Understand what find_element returns
It returns the first matching WebElement with name="search".Step 2: Check the tag_name property
The tag_name of an input field is "input".Final Answer:
"input" -> Option DQuick Check:
Element with name 'search' is input tag [OK]
- Confusing attribute value with tag name
- Assuming find_element returns text content
- Thinking find_element syntax is wrong here
element = driver.find_element(By.name, "password")
Solution
Step 1: Check the locator usage
Locator constants in Selenium are uppercase, so it should beBy.NAME, notBy.name.Step 2: Verify other parts
Method name and quotes are correct; locator type is appropriate for name attribute.Final Answer:
By.name should be By.NAME (case sensitive). -> Option AQuick Check:
Locator constants are uppercase in Selenium [OK]
- Using lowercase locator constants
- Confusing locator types (name vs id)
- Assuming method name is wrong
Solution
Step 1: Locate element by name attribute
Usefind_element(By.NAME, "user_email")to find the input field by its name.Step 2: Enter text into the field
Usesend_keys("test@example.com")to type the email into the field.Final Answer:
element = driver.find_element(By.NAME, "user_email") element.send_keys("test@example.com") -> Option AQuick Check:
Find by name + send_keys to input text [OK]
- Using wrong locator type (ID or CLASS_NAME)
- Using click() instead of send_keys() to enter text
- Not locating element before sending keys
Ready to prove it?
Pass the test and this pattern moves unknown
