0
0
Selenium Pythontesting~20 mins

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

Choose your learning style9 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.