File: input.md | Updated: 11/18/2025
On this page
Introduction
Playwright can interact with HTML Input elements such as text inputs, checkboxes, radio buttons, select options, mouse clicks, type characters, keys and shortcuts as well as upload files and focus elements.
Text input
Using Locator.fill()
is the easiest way to fill out the form fields. It focuses the element and triggers an input event with the entered text. It works for <input>, <textarea> and [contenteditable] elements.
// Text inputpage.getByRole(AriaRole.TEXTBOX).fill("Peter");// Date inputpage.getByLabel("Birth date").fill("2020-02-02");// Time inputpage.getByLabel("Appointment time").fill("13-15");// Local datetime inputpage.getByLabel("Local time").fill("2020-03-02T05:15");
Checkboxes and radio buttons
Using Locator.setChecked()
is the easiest way to check and uncheck a checkbox or a radio button. This method can be used with input[type=checkbox], input[type=radio] and [role=checkbox] elements.
// Check the checkboxpage.getByLabel("I agree to the terms above").check();// Assert the checked stateassertTrue(page.getByLabel("Subscribe to newsletter")).isChecked();// Select the radio buttonpage.getByLabel("XL").check();
Select options
Selects one or multiple options in the <select> element with Locator.selectOption()
. You can specify option value, or label to select. Multiple options can be selected.
// Single selection matching the value or labelpage.getByLabel("Choose a color").selectOption("blue");// Single selection matching the labelpage.getByLabel("Choose a color").selectOption(new SelectOption().setLabel("Blue"));// Multiple selected itemspage.getByLabel("Choose multiple colors").selectOption(new String[] {"red", "green", "blue"});
Mouse click
Performs a simple human click.
// Generic clickpage.getByRole(AriaRole.BUTTON).click();// Double clickpage.getByText("Item").dblclick();// Right clickpage.getByText("Item").click(new Locator.ClickOptions().setButton(MouseButton.RIGHT));// Shift + clickpage.getByText("Item").click(new Locator.ClickOptions().setModifiers(Arrays.asList(KeyboardModifier.SHIFT)));// Ctrl + click on Windows and Linux// Meta + click on macOSpage.getByText("Item").click(new Locator.ClickOptions().setModifiers(Arrays.asList(KeyboardModifier.CONTROL_OR_META)));// Hover over elementpage.getByText("Item").hover();// Click the top left cornerpage.getByText("Item").click(new Locator.ClickOptions().setPosition(0, 0));
Under the hood, this and other pointer-related methods:
display:none, no visibility:hiddenSometimes, apps use non-trivial logic where hovering the element overlays it with another element that intercepts the click. This behavior is indistinguishable from a bug where element gets covered and the click is dispatched elsewhere. If you know this is taking place, you can bypass the actionability checks and force the click:
page.getByRole(AriaRole.BUTTON).click(new Locator.ClickOptions().setForce(true));
If you are not interested in testing your app under the real conditions and want to simulate the click by any means possible, you can trigger the HTMLElement.click()
behavior via simply dispatching a click event on the element with Locator.dispatchEvent()
:
page.getByRole(AriaRole.BUTTON).dispatchEvent("click");
Type characters
caution
Most of the time, you should input text with Locator.fill() . See the Text input section above. You only need to type characters if there is special keyboard handling on the page.
Type into the field character by character, as if it was a user with a real keyboard with Locator.pressSequentially() .
// Press keys one by onepage.locator("#area").pressSequentially("Hello World!");
This method will emit all the necessary keyboard events, with all the keydown, keyup, keypress events in place. You can even specify the optional delay between the key presses to simulate real user behavior.
Keys and shortcuts
// Hit Enterpage.getByText("Submit").press("Enter");// Dispatch Control+Rightpage.getByRole(AriaRole.TEXTBOX).press("Control+ArrowRight");// Press $ sign on keyboardpage.getByRole(AriaRole.TEXTBOX).press("$");
The Locator.press() method focuses the selected element and produces a single keystroke. It accepts the logical key names that are emitted in the keyboardEvent.key property of the keyboard events:
Backquote, Minus, Equal, Backslash, Backspace, Tab, Delete, Escape,ArrowDown, End, Enter, Home, Insert, PageDown, PageUp, ArrowRight,ArrowUp, F1 - F12, Digit0 - Digit9, KeyA - KeyZ, etc.
"a" or "#".Shift, Control, Alt, Meta.Simple version produces a single character. This character is case-sensitive, so "a" and "A" will produce different results.
// <input id=name>page.locator("#name").press("Shift+A");// <input id=name>page.locator("#name").press("Shift+ArrowLeft");
Shortcuts such as "Control+o" or "Control+Shift+T" are supported as well. When specified with the modifier, modifier is pressed and being held while the subsequent key is being pressed.
Note that you still need to specify the capital A in Shift-A to produce the capital character. Shift-a produces a lower-case one as if you had the CapsLock toggled.
Upload files
You can select input files for upload using the Locator.setInputFiles()
method. It expects first argument to point to an input element
with the type "file". Multiple files can be passed in the array. If some of the file paths are relative, they are resolved relative to the current working directory. Empty array clears the selected files.
// Select one filepage.getByLabel("Upload file").setInputFiles(Paths.get("myfile.pdf"));// Select multiple filespage.getByLabel("Upload files").setInputFiles(new Path[] {Paths.get("file1.txt"), Paths.get("file2.txt")});// Select a directorypage.getByLabel("Upload directory").setInputFiles(Paths.get("mydir"));// Remove all the selected filespage.getByLabel("Upload file").setInputFiles(new Path[0]);// Upload buffer from memorypage.getByLabel("Upload file").setInputFiles(new FilePayload( "file.txt", "text/plain", "this is test".getBytes(StandardCharsets.UTF_8)));
If you don't have input element in hand (it is created dynamically), you can handle the Page.onFileChooser(handler) event or use a corresponding waiting method upon your action:
FileChooser fileChooser = page.waitForFileChooser(() -> { page.getByLabel("Upload file").click();});fileChooser.setFiles(Paths.get("myfile.pdf"));
Focus element
For the dynamic pages that handle focus events, you can focus the given element with Locator.focus() .
page.getByLabel("Password").focus();
Drag and Drop
You can perform drag&drop operation with Locator.dragTo() . This method will:
Hover the element that will be dragged.
Press left mouse button.
Move mouse to the element that will receive the drop.
Release left mouse button.
page.locator("#item-to-be-dragged").dragTo(page.locator("#item-to-drop-at"));
If you want precise control over the drag operation, use lower-level methods like Locator.hover() , Mouse.down() , Mouse.move() and Mouse.up() .
page.locator("#item-to-be-dragged").hover();page.mouse().down();page.locator("#item-to-drop-at").hover();page.mouse().up();
note
If your page relies on the dragover event being dispatched, you need at least two mouse moves to trigger it in all browsers. To reliably issue the second mouse move, repeat your Mouse.move()
or Locator.hover()
twice. The sequence of operations would be: hover the drag element, mouse down, hover the drop element, hover the drop element second time, mouse up.
Scrolling
Most of the time, Playwright will automatically scroll for you before doing any actions. Therefore, you do not need to scroll explicitly.
// Scrolls automatically so that button is visiblepage.getByRole(AriaRole.BUTTON).click();
However, in rare cases you might need to manually scroll. For example, you might want to force an "infinite list" to load more elements, or position the page for a specific screenshot. In such a case, the most reliable way is to find an element that you want to make visible at the bottom, and scroll it into view.
// Scroll the footer into view, forcing an "infinite list" to load more contentpage.getByText("Footer text").scrollIntoViewIfNeeded();
If you would like to control the scrolling more precisely, use Mouse.wheel() or Locator.evaluate() :
// Position the mouse and scroll with the mouse wheelpage.getByTestId("scrolling-container").hover();page.mouse.wheel(0, 10);// Alternatively, programmatically scroll a specific elementpage.getByTestId("scrolling-container").evaluate("e => e.scrollTop += 100");