0
0
Selenium Pythontesting~20 mins

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

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