0
0
Selenium Pythontesting~15 mins

Typing text (send_keys) in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify typing text into a search input field
Preconditions (2)
Step 1: Open the browser and navigate to 'https://example.com/search'
Step 2: Locate the search input field by its id 'search-input'
Step 3: Type the text 'Selenium testing' into the search input field
Step 4: Verify that the input field contains the text 'Selenium testing'
✅ Expected Result: The search input field should display the typed text 'Selenium testing'
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the input field's value attribute equals 'Selenium testing'
Best Practices:
Use explicit waits to ensure the input field is visible and interactable before typing
Use By.ID locator for the search input field
Use clear() before send_keys() to avoid appending text
Use unittest or pytest assertions for validation
Automated Solution
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 TestTypingText(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/search')
        self.wait = WebDriverWait(self.driver, 10)

    def test_typing_text(self):
        driver = self.driver
        wait = self.wait
        # Wait until the search input is visible
        search_input = wait.until(EC.visibility_of_element_located((By.ID, 'search-input')))
        # Clear any existing text
        search_input.clear()
        # Type the text
        search_input.send_keys('Selenium testing')
        # Assert the input's value is as expected
        self.assertEqual(search_input.get_attribute('value'), 'Selenium testing')

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

if __name__ == '__main__':
    unittest.main()

This test script uses Selenium WebDriver with Python's unittest framework.

In setUp, it opens the Chrome browser and navigates to the test page.

It waits explicitly for the search input field to be visible using WebDriverWait and expected_conditions.

Before typing, it clears the input field to avoid appending text.

Then it types 'Selenium testing' using send_keys.

Finally, it asserts that the input field's value matches the typed text.

The tearDown method closes the browser after the test.

Common Mistakes - 3 Pitfalls
Not waiting for the input field to be visible before typing
Using hardcoded XPath that is brittle
Not clearing the input field before send_keys
Bonus Challenge

Now add data-driven testing with 3 different input texts: 'Selenium testing', 'QA automation', 'Python scripting'

Show Hint