Bird
Raised Fist0
Selenium Pythontesting~20 mins

Find element by name in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Name Locator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ locator
intermediate
1:30remaining
Identify the correct locator to find an element by name
Which of the following Selenium Python commands correctly finds an element by its name attribute with value username?
Adriver.find_element(By.TAG_NAME, 'username')
Bdriver.find_element(By.ID, 'username')
Cdriver.find_element(By.CLASS_NAME, 'username')
Ddriver.find_element(By.NAME, 'username')
Attempts:
2 left
💡 Hint
The name attribute is different from id, class, or tag.
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium find_element code?
Given the following code snippet, what will happen if no element with name search exists on the page?
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://example.com')
element = driver.find_element(By.NAME, 'search')
print(element.tag_name)
APrints an empty string
BRaises a NoSuchElementException error
CPrints None
DPrints the tag name of the first element with name 'search'
Attempts:
2 left
💡 Hint
If Selenium cannot find the element, it raises an error instead of returning None.
❓ assertion
advanced
2:00remaining
Choose the correct assertion to verify element presence by name
You want to write a test assertion to check that an element with name email is present on the page. Which assertion is correct?
Selenium Python
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

try:
    element = driver.find_element(By.NAME, 'email')
    # Assertion here
except NoSuchElementException:
    assert False, 'Element with name email not found'
Aassert element is not None
Bassert element.tag_name == 'input'
Cassert element.get_attribute('name') == 'email'
Dassert element.text == 'email'
Attempts:
2 left
💡 Hint
Check the attribute value to confirm the correct element was found.
🔧 Debug
advanced
1:30remaining
Find the error in this Selenium code to locate element by name
What is wrong with this Selenium Python code snippet that tries to find an element by name password?
Selenium Python
element = driver.find_element('name', 'password')
AThe locator type should be By.NAME, not a string 'name'
BThe method should be find_elements instead of find_element
CThe attribute 'password' should be passed as a keyword argument
DThe method find_element does not exist in Selenium
Attempts:
2 left
💡 Hint
Selenium expects locator types from the By class, not plain strings.
❓ framework
expert
2:30remaining
Which pytest fixture setup correctly initializes WebDriver for tests using element name locator?
You want to write pytest tests that find elements by name. Which fixture setup is correct to initialize and quit the Chrome WebDriver properly?
A
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

@pytest.fixture(scope='module')
def driver():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()
B
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

@pytest.fixture
async def driver():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()
C
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

@pytest.fixture(scope='session')
async def driver():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()
D
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

@pytest.fixture
async def driver():
    driver = webdriver.Chrome()
    driver.close()
Attempts:
2 left
💡 Hint
Use synchronous fixtures with proper scope and quit the driver after tests.

Practice

(1/5)
1. What does the Selenium method find_element(By.NAME, "username") do?
easy
A. Finds the element with the class name "username".
B. Finds the first element with the name attribute equal to "username".
C. Finds the element with the id attribute "username".
D. Finds all elements with the name attribute equal to "username".

Solution

  1. Step 1: Understand the method purpose

    The method find_element locates a single element matching the given locator.
  2. Step 2: Analyze the locator type

    Using By.NAME targets elements by their name attribute, not id or class.
  3. Final Answer:

    Finds the first element with the name attribute equal to "username". -> Option B
  4. Quick Check:

    find_element(By.NAME, "username") = first element by name [OK]
Hint: Remember: find_element finds one element by attribute [OK]
Common Mistakes:
  • Confusing find_element with find_elements (plural)
  • Using id or class instead of name attribute
  • Assuming it finds multiple elements
2. Which of the following is the correct syntax to find an element by its name attribute "email" in Selenium Python?
easy
A. driver.find_element_by_name("email")
B. driver.find_element(By.ID, "email")
C. driver.find_element(By.NAME, "email")
D. driver.find_element(By.CLASS_NAME, "email")

Solution

  1. Step 1: Recall modern Selenium syntax

    Since Selenium 4, the correct way is to use find_element(By.NAME, "email").
  2. Step 2: Check each option

    driver.find_element_by_name("email") is deprecated syntax, C and D use wrong locator types.
  3. Final Answer:

    driver.find_element(By.NAME, "email") -> Option C
  4. Quick Check:

    Use By.NAME with find_element [OK]
Hint: Use By.NAME, not old find_element_by_name [OK]
Common Mistakes:
  • Using deprecated find_element_by_name method
  • Mixing locator types like ID or CLASS_NAME
  • Forgetting to import By from selenium.webdriver.common.by
3. Given the code snippet:
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"?
medium
A. An error because find_element is incorrect
B. "div"
C. "search"
D. "input"

Solution

  1. Step 1: Understand what find_element returns

    It returns the first matching WebElement with name="search".
  2. Step 2: Check the tag_name property

    The tag_name of an input field is "input".
  3. Final Answer:

    "input" -> Option D
  4. Quick Check:

    Element with name 'search' is input tag [OK]
Hint: tag_name shows HTML tag of found element [OK]
Common Mistakes:
  • Confusing attribute value with tag name
  • Assuming find_element returns text content
  • Thinking find_element syntax is wrong here
4. What is wrong with this code snippet?
element = driver.find_element(By.name, "password")
medium
A. By.name should be By.NAME (case sensitive).
B. The method find_element does not exist.
C. The locator should be By.ID instead of By.name.
D. The quotes around "password" are incorrect.

Solution

  1. Step 1: Check the locator usage

    Locator constants in Selenium are uppercase, so it should be By.NAME, not By.name.
  2. Step 2: Verify other parts

    Method name and quotes are correct; locator type is appropriate for name attribute.
  3. Final Answer:

    By.name should be By.NAME (case sensitive). -> Option A
  4. Quick Check:

    Locator constants are uppercase in Selenium [OK]
Hint: Locator constants like By.NAME are uppercase [OK]
Common Mistakes:
  • Using lowercase locator constants
  • Confusing locator types (name vs id)
  • Assuming method name is wrong
5. You want to fill a form field with name "user_email" using Selenium Python. Which code snippet correctly finds the element and enters the email "test@example.com"?
hard
A. element = driver.find_element(By.NAME, "user_email") element.send_keys("test@example.com")
B. element = driver.find_element(By.ID, "user_email") element.send_keys("test@example.com")
C. element = driver.find_element(By.CLASS_NAME, "user_email") element.send_keys("test@example.com")
D. element = driver.find_element(By.NAME, "user_email") element.click()

Solution

  1. Step 1: Locate element by name attribute

    Use find_element(By.NAME, "user_email") to find the input field by its name.
  2. Step 2: Enter text into the field

    Use send_keys("test@example.com") to type the email into the field.
  3. Final Answer:

    element = driver.find_element(By.NAME, "user_email") element.send_keys("test@example.com") -> Option A
  4. Quick Check:

    Find by name + send_keys to input text [OK]
Hint: Use send_keys after find_element to type text [OK]
Common Mistakes:
  • Using wrong locator type (ID or CLASS_NAME)
  • Using click() instead of send_keys() to enter text
  • Not locating element before sending keys
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard