Skip to content
CodeKruPro

Type Uppercase with the Shift Key

62/80

Type Uppercase with the Shift Key

Hold Shift with key_down, type hello, then release Shift with key_up. In this browser the field receives HELLO.

Task:

  1. Click the first #firstName
  2. Hold Shift and type hello
  3. Print the value

Print the lines below exactly.

Expected output:

Typed: HELLO

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

  1. Build an ActionChains object around the driver.
  2. Start the chain with a click on the field, so it has focus before any key arrives.
  3. Press Shift with key_down, send the lowercase word, and release Shift with key_up.
  4. Call perform(), then read the field's value attribute 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_up is 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.

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.

main.py
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()