0
0
Selenium Pythontesting~15 mins

Key combinations (key_down, key_up) in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Test keyboard shortcut Ctrl + A to select all text in input field
Preconditions (2)
Step 1: Click on the input field with id 'input-text' to focus it.
Step 2: Press and hold the Control key (Ctrl).
Step 3: Press the 'A' key while holding Ctrl.
Step 4: Release the Control key.
Step 5: Verify that all text in the input field is selected.
✅ Expected Result: All text 'Hello World' inside the input field is selected after pressing Ctrl + A.
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the input field is focused after clicking.
Verify that the entire text inside the input field is selected after pressing Ctrl + A.
Best Practices:
Use explicit waits to ensure the input field is clickable before interacting.
Use ActionChains for key_down and key_up to simulate key combinations.
Use clear and maintainable locators (By.ID).
Avoid hardcoded sleeps; use WebDriverWait instead.
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Setup WebDriver (assumes chromedriver is in PATH)
driver = webdriver.Chrome()

try:
    driver.get('https://example.com')  # Replace with actual URL containing the input field

    wait = WebDriverWait(driver, 10)
    input_field = wait.until(EC.element_to_be_clickable((By.ID, 'input-text')))

    # Click to focus the input field
    input_field.click()

    # Verify input field is focused
    assert driver.switch_to.active_element == input_field, "Input field is not focused"

    # Use ActionChains to press Ctrl + A
    actions = ActionChains(driver)
    actions.key_down('\ue009').send_keys('a').key_up('\ue009').perform()  # \ue009 is CONTROL key

    # Verify all text is selected
    # Get selection start and end from the input field using JavaScript
    selection_start = driver.execute_script('return arguments[0].selectionStart;', input_field)
    selection_end = driver.execute_script('return arguments[0].selectionEnd;', input_field)
    input_length = len(input_field.get_attribute('value'))

    assert selection_start == 0 and selection_end == input_length, "Not all text is selected"

finally:
    driver.quit()

This script opens the webpage and waits until the input field with id 'input-text' is clickable. It clicks the input field to focus it and asserts that the field is focused by comparing the active element.

Then, it uses Selenium's ActionChains to simulate pressing the Control key down, pressing 'a', and releasing Control key, which triggers the 'select all' shortcut.

To verify the selection, it runs JavaScript to get the selection start and end positions inside the input field. If the selection covers the entire text length, the test passes.

Explicit waits ensure the element is ready before interaction, and assertions confirm each step's success.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
{'mistake': "Using hardcoded XPath selectors like '//input[1]'", 'why_bad': 'Hardcoded XPath is fragile and can break if the page structure changes.', 'correct_approach': 'Use stable locators like By.ID or By.NAME for better maintainability.'}
Not releasing the Control key after key_down
Not verifying the input field is focused before sending keys
Bonus Challenge

Now add data-driven testing with 3 different input texts to verify Ctrl + A selects all text correctly.

Show Hint