Skip to content
CodeKruPro

Select all and replace text with the keyboard

Automation taskMediumAsked in: Accenture, Capgemini, Infosys, TCS, Wipro

Asked as: "How do you press Ctrl+A, Backspace or Enter in Selenium?" Send special keys to the field from the Keys class. Remember that the shortcut uses Cmd instead of Ctrl on a Mac. Here you type Hello World into a field, select all, delete it, type Selenium and print the final value.

Keyboard - Special Keys

Send Special Keys

The Keys class provides constants for special keys like ENTER, TAB, BACKSPACE, CONTROL, etc.

You can use them with sendKeys() to simulate keyboard shortcuts.

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Type "Hello World" in the firstName field
  3. Select all text using Ctrl+A (Keys.chord(Keys.CONTROL, "a"))
  4. Delete the selected text using BACKSPACE
  5. Type "Selenium" in the now-empty field
  6. Print the final value

Expected output:

Final value: Selenium