We use key combinations to simulate pressing multiple keys together, like Ctrl+C for copy. This helps test how web pages respond to keyboard shortcuts.
0
0
Key combinations (key_down, key_up) in Selenium Python
Introduction
Testing if Ctrl+C copies text on a webpage.
Checking if Shift+Tab moves focus backward in a form.
Verifying keyboard shortcuts like Ctrl+S to save.
Simulating pressing Ctrl+Alt+Delete or other combos in web apps.
Testing accessibility features that rely on key combos.
Syntax
Selenium Python
from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys # driver is your WebDriver instance actions = ActionChains(driver) actions.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()
Use key_down() to press and hold a key.
Use key_up() to release the key.
Examples
This types 'HELLO' by holding Shift while typing.
Selenium Python
actions.key_down(Keys.SHIFT).send_keys('hello').key_up(Keys.SHIFT).perform()This simulates pressing Ctrl+C to copy.
Selenium Python
actions.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()This presses Alt+Shift+S together, then releases them.
Selenium Python
actions.key_down(Keys.ALT).key_down(Keys.SHIFT).send_keys('s').key_up(Keys.SHIFT).key_up(Keys.ALT).perform()Sample Program
This script opens a test page with an input box, types 'hello world' with 'world' in uppercase by holding Shift, then selects all text and copies it using Ctrl+A and Ctrl+C key combinations.
Selenium Python
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys import time # Setup WebDriver (using Chrome here) driver = webdriver.Chrome() driver.get('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_input_test') # Switch to iframe where input box is iframe = driver.find_element('id', 'iframeResult') driver.switch_to.frame(iframe) input_box = driver.find_element('xpath', '//input[@type="text"]') input_box.click() actions = ActionChains(driver) # Type lowercase 'hello' actions.send_keys('hello ').perform() # Hold Shift and type 'world' to get uppercase actions.key_down(Keys.SHIFT).send_keys('world').key_up(Keys.SHIFT).perform() # Wait to see result print('Typed text in input box:') print(input_box.get_attribute('value')) # Clear input box input_box.clear() # Simulate Ctrl+A (select all) then Ctrl+C (copy) actions.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform() actions.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform() print('Performed Ctrl+A and Ctrl+C key combinations.') # Cleanup time.sleep(2) driver.quit()
OutputSuccess
Important Notes
Time complexity is not applicable as these are UI actions.
Always release keys with key_up() to avoid stuck keys in tests.
Use key combinations to test keyboard shortcuts and accessibility.
Summary
Use key_down() and key_up() to simulate pressing and releasing keys.
Combine with send_keys() to type while holding keys like Shift or Ctrl.
This helps test keyboard shortcuts and user interactions on web pages.