0
0
Selenium Pythontesting~10 mins

Find element by name in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page, finds an input element by its name attribute, enters text, and verifies the input value.

Test Code - Selenium
Selenium Python
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
import unittest

class TestFindElementByName(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/login')

    def test_find_element_by_name(self):
        driver = self.driver
        # Wait until the input element with name 'username' is present
        username_input = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.NAME, 'username'))
        )
        username_input.clear()
        username_input.send_keys('testuser')
        # Verify the input value
        self.assertEqual(username_input.get_attribute('value'), 'testuser')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open, ready to load URL-PASS
2Navigates to 'https://example.com/login'Login page is loaded with input fields-PASS
3Waits until element with name 'username' is presentInput field with name 'username' is visible on pageElement located by name 'username' is foundPASS
4Clears the input field and types 'testuser'Input field now contains text 'testuser'-PASS
5Checks that input field value equals 'testuser'Input field value is 'testuser'Assert input value == 'testuser'PASS
6Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: Element with name 'username' is not found within 10 seconds
Execution Trace Quiz - 3 Questions
Test your understanding
Which Selenium method is used to find the element by name?
Adriver.find_element(By.CLASS_NAME, 'username')
Bdriver.find_element_by_id('username')
Cdriver.find_element(By.NAME, 'username')
Ddriver.find_element_by_tag_name('input')
Key Result
Always use explicit waits like WebDriverWait with expected conditions to reliably find elements before interacting with them.