Bird
Raised Fist0
Selenium Pythontesting~20 mins

Key combinations (key_down, key_up) in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Key Combination Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium Python code snippet?
Consider the following Selenium Python code that sends a key combination to an input field. What will be the final text entered in the input field?
Selenium Python
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

browser = Chrome()
browser.get('https://example.com')
input_field = browser.find_element(By.ID, 'search')
actions = ActionChains(browser)
actions.key_down(Keys.SHIFT, input_field).send_keys('hello').key_up(Keys.SHIFT, input_field).perform()
AHELLO
Bhello
CHeLlO
DError: key_up called without key_down
Attempts:
2 left
💡 Hint
Think about what holding down SHIFT does to lowercase letters.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies that CTRL+A was sent to select all text?
You want to verify that sending CTRL+A to an input field selects all text. Which assertion correctly checks this behavior after sending the keys?
Selenium Python
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

input_field = driver.find_element(By.ID, 'text')
actions = ActionChains(driver)
actions.click(input_field).key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()

# Which assertion below correctly verifies the selection?
Aassert driver.execute_script('return window.getSelection().toString()') == input_field.get_attribute('value')
Bassert input_field.get_attribute('value') == 'selected'
Cassert input_field.text == input_field.get_attribute('value')
Dassert input_field.is_selected() is True
Attempts:
2 left
💡 Hint
Selection is a browser property, not an input attribute.
❓ locator
advanced
2:00remaining
Identify the best locator for an input field to send key combinations
You need to send key combinations to an input field that has dynamic IDs but a stable placeholder attribute 'Enter your name'. Which locator is best for this purpose?
Adriver.find_element(By.XPATH, '//input[@id="dynamic123"]')
Bdriver.find_element(By.ID, 'user_input')
Cdriver.find_element(By.CSS_SELECTOR, 'input[placeholder="Enter your name"]')
Ddriver.find_element(By.CLASS_NAME, 'input-field')
Attempts:
2 left
💡 Hint
Dynamic IDs change, so avoid them. Use stable attributes.
🔧 Debug
advanced
2:00remaining
Why does this Selenium key combination code raise an error?
Examine the code below. It raises an error when run. What is the cause?
Selenium Python
actions = ActionChains(driver)
actions.key_down(Keys.CONTROL).send_keys('c').perform()
actions.key_up(Keys.CONTROL).perform()
Akey_up called after perform(), so CONTROL is never released before next action
Bkey_down and key_up must be chained before perform() in one action chain
Csend_keys('c') must be called before key_down
DKeys.CONTROL is not a valid key for key_down
Attempts:
2 left
💡 Hint
ActionChains perform() executes the chain and resets it.
❓ framework
expert
3:00remaining
How to implement a reusable method to send SHIFT+text using Selenium Python?
You want to create a reusable Python method using Selenium that sends SHIFT+text (uppercase) to any input element. Which implementation is correct and follows best practices?
A
def send_shift_text(element, text):
    actions = ActionChains(element._parent)
    actions.key_down(Keys.SHIFT, element).send_keys(text).key_up(Keys.SHIFT, element).perform()
B
def send_shift_text(element, text):
    actions = ActionChains(element._parent)
    actions.key_down(Keys.SHIFT).send_keys(text).key_up(Keys.SHIFT).perform()
C
def send_shift_text(element, text):
    element.send_keys(Keys.SHIFT + text)
D
def send_shift_text(element, text):
    actions = ActionChains(element._parent)
    actions.click(element).key_down(Keys.SHIFT).send_keys(text).key_up(Keys.SHIFT).perform()
Attempts:
2 left
💡 Hint
Always click the element before sending keys to ensure focus.

Practice

(1/5)
1. What does the key_down() method do in Selenium Python?
easy
A. Simulates pressing and holding a key down
B. Simulates releasing a key
C. Types text into an input field
D. Clicks a button on the webpage

Solution

  1. Step 1: Understand the purpose of key_down()

    This method is used to simulate pressing and holding a keyboard key down in Selenium.
  2. Step 2: Differentiate from other methods

    key_up() releases the key, send_keys() types text, and clicking is unrelated.
  3. Final Answer:

    Simulates pressing and holding a key down -> Option A
  4. Quick Check:

    key_down() = press and hold key [OK]
Hint: Remember: key_down holds key, key_up releases it [OK]
Common Mistakes:
  • Confusing key_down with key_up
  • Thinking key_down types text
  • Mixing key_down with mouse click actions
2. Which of the following is the correct syntax to hold the SHIFT key and type 'hello' using Selenium Python's ActionChains?
easy
A. actions.key_down(Keys.SHIFT).send_keys('hello').key_up(Keys.SHIFT).perform()
B. actions.send_keys('hello').key_down(Keys.SHIFT).key_up(Keys.SHIFT).perform()
C. actions.key_up(Keys.SHIFT).send_keys('hello').key_down(Keys.SHIFT).perform()
D. actions.key_down('hello').send_keys(Keys.SHIFT).key_up('hello').perform()

Solution

  1. Step 1: Identify correct method order

    To hold SHIFT while typing, first press SHIFT down, then send keys, then release SHIFT.
  2. Step 2: Check method arguments

    Keys.SHIFT is the correct key constant; 'hello' is the string to type.
  3. Final Answer:

    actions.key_down(Keys.SHIFT).send_keys('hello').key_up(Keys.SHIFT).perform() -> Option A
  4. Quick Check:

    Hold SHIFT before typing, release after [OK]
Hint: Hold key_down before send_keys, release with key_up after [OK]
Common Mistakes:
  • Calling key_up before send_keys
  • Using string instead of Keys.SHIFT
  • Wrong method chaining order
3. What will be the output of the following Selenium Python code snippet?
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

actions = ActionChains(driver)
actions.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()

Assuming the driver is focused on a text input field.
medium
A. Types the letter 'a' without any selection
B. Throws a syntax error due to missing parentheses
C. Selects all text inside the input field
D. Clears the input field content

Solution

  1. Step 1: Understand the key combination

    Holding CONTROL and pressing 'a' is the common shortcut to select all text.
  2. Step 2: Analyze the code effect

    The code holds CONTROL, sends 'a', then releases CONTROL, triggering select all in the input.
  3. Final Answer:

    Selects all text inside the input field -> Option C
  4. Quick Check:

    Ctrl + A = select all [OK]
Hint: Ctrl + A selects all text in input fields [OK]
Common Mistakes:
  • Thinking it types lowercase 'a' only
  • Confusing key_down with key_up order
  • Assuming it clears input instead of selecting
4. Identify the error in this Selenium Python code snippet that tries to hold CTRL and press 'c':
actions = ActionChains(driver)
actions.key_down('CONTROL').send_keys('c').key_up('CONTROL').perform()
medium
A. Missing perform() method call at the end
B. Using string 'CONTROL' instead of Keys.CONTROL constant
C. send_keys should be called before key_down
D. key_up should be called before key_down

Solution

  1. Step 1: Check key constants usage

    Keys like CONTROL must be referenced from selenium.webdriver.common.keys.Keys, not as plain strings.
  2. Step 2: Verify method chaining and perform call

    perform() is correctly called; send_keys order is correct; key_up after key_down is correct.
  3. Final Answer:

    Using string 'CONTROL' instead of Keys.CONTROL constant -> Option B
  4. Quick Check:

    Use Keys.CONTROL, not 'CONTROL' string [OK]
Hint: Always use Keys constants, not plain strings for special keys [OK]
Common Mistakes:
  • Using plain strings for special keys
  • Forgetting to call perform()
  • Wrong order of key_down and send_keys
5. You want to test a web app shortcut that requires pressing CTRL + SHIFT + S simultaneously using Selenium Python. Which code snippet correctly performs this action?
hard
A. actions.key_down(Keys.SHIFT).send_keys('s').key_down(Keys.CONTROL).key_up(Keys.SHIFT).key_up(Keys.CONTROL).perform()
B. actions.send_keys(Keys.CONTROL + Keys.SHIFT + 's').perform()
C. actions.key_down('CONTROL').send_keys('S').key_up('SHIFT').perform()
D. actions.key_down(Keys.CONTROL).key_down(Keys.SHIFT).send_keys('s').key_up(Keys.SHIFT).key_up(Keys.CONTROL).perform()

Solution

  1. Step 1: Press keys in correct order

    Hold CTRL down, then SHIFT down, then send 's' key.
  2. Step 2: Release keys in reverse order

    Release SHIFT first, then CTRL, then perform the action.
  3. Step 3: Validate method chaining and constants

    Use Keys constants, not strings, and chain methods properly.
  4. Final Answer:

    actions.key_down(Keys.CONTROL).key_down(Keys.SHIFT).send_keys('s').key_up(Keys.SHIFT).key_up(Keys.CONTROL).perform() -> Option D
  5. Quick Check:

    Hold CTRL+SHIFT, send 's', release SHIFT then CTRL [OK]
Hint: Hold keys in order, release in reverse, then perform [OK]
Common Mistakes:
  • Using string keys instead of Keys constants
  • Releasing keys in wrong order
  • Trying to combine keys inside send_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