About this lesson
Hold the Shift key in Selenium Python with ActionChains key_down and key_up
Selenium Python AssignmentsAdvancedMouse and Keyboard
How it works
A normal send_keys call presses and releases each key at once. Holding a key across several actions needs ActionChains, which builds a list of low-level input steps and plays them in order when you call perform(). key_down presses a key and keeps it pressed. key_up releases it. Between the two, every character you send behaves as if the key were held.
Each method returns the chain itself, so you can write click, press, type, release and perform on one line. Nothing happens until perform() runs. A chain I built without it left the field empty.
Keys go to whichever element has focus, and nothing focuses the field for you. With no click first, the field stayed empty. Also, the key state belongs to the browser session, not the chain object. When I left Shift down, a later chain typed capitals too, until a key_up released it.
About the assignment
You type a lowercase word into the first matching field while Shift is held, then show what the field received. The printed value differs from what you typed.
What you will do
- Build an
ActionChainsobject around the driver. - Start the chain with a click on the field, so it has focus before any key arrives.
- Press Shift with
key_down, send the lowercase word, and release Shift withkey_up. - Call
perform(), then read the field'svalueattribute and print the label with that value.
Common mistakes
Forgetting perform()
No error appears, but no key is pressed. The field is still empty when you read it.
Skipping the click
Without focus, the keystrokes have nowhere to go, and the value you read is an empty string.
Leaving Shift held
If
key_upis missing, later typing in the same session keeps coming out in capitals. Keep the release in the same chain.
In a real test suite
For one-off capitals, teams often pass the modifier straight to send_keys with the text. The explicit down and up pair is for longer sequences, such as holding Shift while arrow keys extend a selection.
The next lesson starts a new group: running JavaScript from your script.
Related lessons
- Select All and Replace Text with the KeyboardUse a modifier together with a letter in a single
send_keyscall. - Double-Click a ButtonAnother
ActionChainslesson whereperform()triggers the whole chain. - Click and Hold a BoxAnother press-and-hold action, this time with the mouse button.
To try one of these calls on any page without a task, open the Online Selenium Python Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Elements/TextFields")
# Type hello while Shift is held, into the first #firstName
finally:
driver.quit()