Find element by name in Selenium Python - Build an Automation Script
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException # Initialize the Chrome WebDriver with webdriver.Chrome() as driver: driver.get('https://example.com/login') try: # Wait up to 10 seconds for the login button to be present and clickable wait = WebDriverWait(driver, 10) login_button = wait.until(EC.element_to_be_clickable((By.NAME, 'loginButton'))) # Assert the button is displayed assert login_button.is_displayed(), 'Login button is not visible' # Click the login button login_button.click() # If needed, add further assertions here except TimeoutException: assert False, 'Login button was not found or not clickable within 10 seconds'
This script uses Selenium WebDriver with Python to automate the manual test case.
First, it opens the Chrome browser and navigates to the login page URL.
It uses an explicit wait to wait up to 10 seconds for the login button to be clickable, locating it by its name attribute 'loginButton'.
It asserts that the button is visible before clicking it to ensure the element is interactable.
If the button is not found or not clickable within the wait time, the test fails with a clear message.
The with statement ensures the browser closes automatically after the test finishes.
Now add data-driven testing with 3 different name attributes for buttons to verify each is clickable
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
