2021-01-01 15:17:27 -08:00
---
id: input
2022-09-08 14:48:13 +02:00
title: "Actions"
2021-01-01 15:17:27 -08:00
---
2020-12-30 18:04:51 -08:00
2023-10-06 15:08:51 +02:00
## Introduction
2022-09-07 17:19:54 +02:00
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.
2020-12-30 18:04:51 -08:00
## Text input
2022-10-03 17:02:46 -07:00
Using [`method: 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.
2020-12-30 18:04:51 -08:00
```js
// Text input
2022-10-03 17:02:46 -07:00
await page.getByRole('textbox').fill('Peter');
2020-12-30 18:04:51 -08:00
// Date input
2022-10-04 09:29:26 -08:00
await page.getByLabel('Birth date').fill('2020-02-02');
2020-12-30 18:04:51 -08:00
// Time input
2022-10-04 09:29:26 -08:00
await page.getByLabel('Appointment time').fill('13:15');
2020-12-30 18:04:51 -08:00
// Local datetime input
2022-10-04 09:29:26 -08:00
await page.getByLabel('Local time').fill('2020-03-02T05:15');
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
// Text input
2022-11-30 13:46:33 -08:00
page.getByRole(AriaRole.TEXTBOX).fill("Peter");
2021-03-01 09:18:44 -08:00
// Date input
2022-10-04 09:29:26 -08:00
page.getByLabel("Birth date").fill("2020-02-02");
2021-03-01 09:18:44 -08:00
// Time input
2022-10-04 09:29:26 -08:00
page.getByLabel("Appointment time").fill("13-15");
2021-03-01 09:18:44 -08:00
// Local datetime input
2022-10-04 09:29:26 -08:00
page.getByLabel("Local time").fill("2020-03-02T05:15");
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Text input
2022-10-03 17:02:46 -07:00
await page.get_by_role("textbox").fill("Peter")
2021-01-08 17:39:33 +01:00
# Date input
2022-10-04 09:29:26 -08:00
await page.get_by_label("Birth date").fill("2020-02-02")
2021-01-08 17:39:33 +01:00
# Time input
2022-10-04 09:29:26 -08:00
await page.get_by_label("Appointment time").fill("13:15")
2021-01-08 17:39:33 +01:00
# Local datetime input
2022-10-04 09:29:26 -08:00
await page.get_by_label("Local time").fill("2020-03-02T05:15")
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Text input
2022-10-03 17:02:46 -07:00
page.get_by_role("textbox").fill("Peter")
2021-01-08 17:39:33 +01:00
# Date input
2022-10-04 09:29:26 -08:00
page.get_by_label("Birth date").fill("2020-02-02")
2021-01-08 17:39:33 +01:00
# Time input
2022-10-04 09:29:26 -08:00
page.get_by_label("Appointment time").fill("13:15")
2021-01-08 17:39:33 +01:00
# Local datetime input
2022-10-04 09:29:26 -08:00
page.get_by_label("Local time").fill("2020-03-02T05:15")
2021-01-08 17:39:33 +01:00
```
2021-05-15 10:56:10 -07:00
```csharp
// Text input
2022-11-30 13:46:33 -08:00
await page.GetByRole(AriaRole.Textbox).FillAsync("Peter");
2021-05-15 10:56:10 -07:00
// Date input
2022-10-04 09:29:26 -08:00
await page.GetByLabel("Birth date").FillAsync("2020-02-02");
2021-05-15 10:56:10 -07:00
// Time input
2022-10-04 09:29:26 -08:00
await page.GetByLabel("Appointment time").FillAsync("13-15");
2021-05-15 10:56:10 -07:00
// Local datetime input
2022-10-04 09:29:26 -08:00
await page.GetByLabel("Local time").FillAsync("2020-03-02T05:15");
2021-05-15 10:56:10 -07:00
```
2020-12-30 18:04:51 -08:00
## Checkboxes and radio buttons
2022-10-03 17:02:46 -07:00
Using [`method: 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.
2020-12-30 18:04:51 -08:00
```js
// Check the checkbox
2022-10-04 09:29:26 -08:00
await page.getByLabel('I agree to the terms above').check();
2020-12-30 18:04:51 -08:00
2021-01-17 21:09:40 -08:00
// Assert the checked state
2023-11-15 19:51:30 +01:00
expect(page.getByLabel('Subscribe to newsletter')).toBeChecked();
2020-12-30 18:04:51 -08:00
// Select the radio button
2022-10-04 09:29:26 -08:00
await page.getByLabel('XL').check();
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
// Check the checkbox
2022-10-04 09:29:26 -08:00
page.getByLabel("I agree to the terms above").check();
2021-03-01 09:18:44 -08:00
// Assert the checked state
2023-11-15 19:51:30 +01:00
assertTrue(page.getByLabel("Subscribe to newsletter")).isChecked();
2021-03-01 09:18:44 -08:00
// Select the radio button
2022-10-04 09:29:26 -08:00
page.getByLabel("XL").check();
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Check the checkbox
2022-10-04 09:29:26 -08:00
await page.get_by_label('I agree to the terms above').check()
2021-01-08 17:39:33 +01:00
2021-01-17 21:09:40 -08:00
# Assert the checked state
2023-11-15 19:51:30 +01:00
await expect(page.get_by_label('Subscribe to newsletter')).to_be_checked()
2021-01-08 17:39:33 +01:00
# Select the radio button
2022-10-04 09:29:26 -08:00
await page.get_by_label('XL').check()
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Check the checkbox
2022-10-04 09:29:26 -08:00
page.get_by_label('I agree to the terms above').check()
2021-01-08 17:39:33 +01:00
2021-01-17 21:09:40 -08:00
# Assert the checked state
2023-11-15 19:51:30 +01:00
expect(page.get_by_label('Subscribe to newsletter')).to_be_checked()
2021-01-08 17:39:33 +01:00
# Select the radio button
2022-10-04 09:29:26 -08:00
page.get_by_label('XL').check()
2021-01-08 17:39:33 +01:00
```
2021-05-15 10:56:10 -07:00
```csharp
// Check the checkbox
2022-10-04 09:29:26 -08:00
await page.GetByLabel("I agree to the terms above").CheckAsync();
2021-05-15 10:56:10 -07:00
// Assert the checked state
2023-11-15 19:51:30 +01:00
await Expect(page.GetByLabel("Subscribe to newsletter")).ToBeCheckedAsync();
2021-05-15 10:56:10 -07:00
// Select the radio button
2022-10-04 09:29:26 -08:00
await page.GetByLabel("XL").CheckAsync();
2021-05-15 10:56:10 -07:00
```
2020-12-30 18:04:51 -08:00
## Select options
2022-09-07 17:19:54 +02:00
Selects one or multiple options in the `<select>` element with [`method: Locator.selectOption` ].
2022-07-12 22:39:31 +02:00
You can specify option `value` , or `label` to select. Multiple options can be selected.
2020-12-30 18:04:51 -08:00
```js
2023-09-14 16:41:36 -07:00
// Single selection matching the value or label
2022-10-04 09:29:26 -08:00
await page.getByLabel('Choose a color').selectOption('blue');
2020-12-30 18:04:51 -08:00
// Single selection matching the label
2022-10-04 09:29:26 -08:00
await page.getByLabel('Choose a color').selectOption({ label: 'Blue' });
2020-12-30 18:04:51 -08:00
// Multiple selected items
2022-10-04 09:29:26 -08:00
await page.getByLabel('Choose multiple colors').selectOption(['red', 'green', 'blue']);
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
2023-09-14 16:41:36 -07:00
// Single selection matching the value or label
2022-10-04 09:29:26 -08:00
page.getByLabel("Choose a color").selectOption("blue");
2021-03-01 09:18:44 -08:00
// Single selection matching the label
2022-10-04 09:29:26 -08:00
page.getByLabel("Choose a color").selectOption(new SelectOption().setLabel("Blue"));
2021-03-01 09:18:44 -08:00
// Multiple selected items
2022-10-04 09:29:26 -08:00
page.getByLabel("Choose multiple colors").selectOption(new String[] {"red", "green", "blue"});
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2023-09-14 16:41:36 -07:00
# Single selection matching the value or label
2022-10-04 09:29:26 -08:00
await page.get_by_label('Choose a color').select_option('blue')
2021-01-08 17:39:33 +01:00
# Single selection matching the label
2022-10-04 09:29:26 -08:00
await page.get_by_label('Choose a color').select_option(label='Blue')
2021-01-08 17:39:33 +01:00
# Multiple selected items
2022-10-04 09:29:26 -08:00
await page.get_by_label('Choose multiple colors').select_option(['red', 'green', 'blue'])
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2023-09-14 16:41:36 -07:00
# Single selection matching the value or label
2022-10-04 09:29:26 -08:00
page.get_by_label('Choose a color').select_option('blue')
2021-01-08 17:39:33 +01:00
# Single selection matching the label
2022-10-04 09:29:26 -08:00
page.get_by_label('Choose a color').select_option(label='Blue')
2021-01-08 17:39:33 +01:00
# Multiple selected items
2022-10-04 09:29:26 -08:00
page.get_by_label('Choose multiple colors').select_option(['red', 'green', 'blue'])
2021-01-08 17:39:33 +01:00
```
2021-05-15 10:56:10 -07:00
```csharp
2023-09-14 16:41:36 -07:00
// Single selection matching the value or label
2022-10-04 09:29:26 -08:00
await page.GetByLabel("Choose a color").SelectOptionAsync("blue");
2021-05-15 10:56:10 -07:00
// Single selection matching the label
2023-06-26 18:21:14 +02:00
await page.GetByLabel("Choose a color").SelectOptionAsync(new SelectOptionValue { Label = "blue" });
2021-05-15 10:56:10 -07:00
// Multiple selected items
2022-10-04 09:29:26 -08:00
await page.GetByLabel("Choose multiple colors").SelectOptionAsync(new[] { "blue", "green", "red" });
2021-05-15 10:56:10 -07:00
```
2020-12-30 18:04:51 -08:00
## Mouse click
Performs a simple human click.
```js
// Generic click
2022-10-03 17:02:46 -07:00
await page.getByRole('button').click();
2020-12-30 18:04:51 -08:00
// Double click
2022-10-03 17:02:46 -07:00
await page.getByText('Item').dblclick();
2020-12-30 18:04:51 -08:00
// Right click
2022-10-03 17:02:46 -07:00
await page.getByText('Item').click({ button: 'right' });
2020-12-30 18:04:51 -08:00
// Shift + click
2022-10-03 17:02:46 -07:00
await page.getByText('Item').click({ modifiers: ['Shift'] });
2020-12-30 18:04:51 -08:00
2024-04-29 08:15:12 -07:00
// Ctrl + click or Windows and Linux
// Meta + click on macOS
await page.getByText('Item').click({ modifiers: ['ControlOrMeta'] });
2020-12-30 18:04:51 -08:00
// Hover over element
2022-10-03 17:02:46 -07:00
await page.getByText('Item').hover();
2020-12-30 18:04:51 -08:00
// Click the top left corner
2023-06-27 11:53:53 +02:00
await page.getByText('Item').click({ position: { x: 0, y: 0 } });
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
// Generic click
2022-11-30 13:46:33 -08:00
page.getByRole(AriaRole.BUTTON).click();
2021-03-01 09:18:44 -08:00
// Double click
2022-10-03 17:02:46 -07:00
page.getByText("Item").dblclick();
2021-03-01 09:18:44 -08:00
// Right click
2022-10-03 17:02:46 -07:00
page.getByText("Item").click(new Locator.ClickOptions().setButton(MouseButton.RIGHT));
2021-03-01 09:18:44 -08:00
// Shift + click
2022-10-03 17:02:46 -07:00
page.getByText("Item").click(new Locator.ClickOptions().setModifiers(Arrays.asList(KeyboardModifier.SHIFT)));
2021-03-01 09:18:44 -08:00
2024-04-29 08:15:12 -07:00
// Ctrl + click or Windows and Linux
// Meta + click on macOS
page.getByText("Item").click(new Locator.ClickOptions().setModifiers(Arrays.asList(KeyboardModifier.CONTROL_OR_META)));
2021-03-01 09:18:44 -08:00
// Hover over element
2022-10-03 17:02:46 -07:00
page.getByText("Item").hover();
2021-03-01 09:18:44 -08:00
// Click the top left corner
2022-10-03 17:02:46 -07:00
page.getByText("Item").click(new Locator.ClickOptions().setPosition(0, 0));
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Generic click
2022-10-03 17:02:46 -07:00
await page.get_by_role("button").click()
2021-01-08 17:39:33 +01:00
# Double click
2022-10-03 17:02:46 -07:00
await page.get_by_text("Item").dblclick()
2021-01-08 17:39:33 +01:00
# Right click
2022-10-03 17:02:46 -07:00
await page.get_by_text("Item").click(button="right")
2021-01-08 17:39:33 +01:00
# Shift + click
2022-10-03 17:02:46 -07:00
await page.get_by_text("Item").click(modifiers=["Shift"])
2021-01-08 17:39:33 +01:00
2024-04-29 08:15:12 -07:00
# Ctrl + click or Windows and Linux
# Meta + click on macOS
await page.get_by_text("Item").click(modifiers=["ControlOrMeta"])
2021-01-08 17:39:33 +01:00
# Hover over element
2022-10-03 17:02:46 -07:00
await page.get_by_text("Item").hover()
2021-01-08 17:39:33 +01:00
# Click the top left corner
2022-10-03 17:02:46 -07:00
await page.get_by_text("Item").click(position={ "x": 0, "y": 0})
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Generic click
2022-10-03 17:02:46 -07:00
page.get_by_role("button").click()
2021-01-08 17:39:33 +01:00
# Double click
2022-10-03 17:02:46 -07:00
page.get_by_text("Item").dblclick()
2021-01-08 17:39:33 +01:00
# Right click
2022-10-03 17:02:46 -07:00
page.get_by_text("Item").click(button="right")
2021-01-08 17:39:33 +01:00
# Shift + click
2022-10-03 17:02:46 -07:00
page.get_by_text("Item").click(modifiers=["Shift"])
2021-01-08 17:39:33 +01:00
# Hover over element
2022-10-03 17:02:46 -07:00
page.get_by_text("Item").hover()
2021-01-08 17:39:33 +01:00
# Click the top left corner
2022-10-03 17:02:46 -07:00
page.get_by_text("Item").click(position={ "x": 0, "y": 0})
2021-01-08 17:39:33 +01:00
```
2021-05-15 10:56:10 -07:00
```csharp
// Generic click
2022-11-30 13:46:33 -08:00
await page.GetByRole(AriaRole.Button).ClickAsync();
2021-05-15 10:56:10 -07:00
// Double click
2022-10-03 17:02:46 -07:00
await page.GetByText("Item").DblClickAsync();
2021-05-15 10:56:10 -07:00
// Right click
2022-10-03 17:02:46 -07:00
await page.GetByText("Item").ClickAsync(new() { Button = MouseButton.Right });
2021-05-15 10:56:10 -07:00
// Shift + click
2022-10-03 17:02:46 -07:00
await page.GetByText("Item").ClickAsync(new() { Modifiers = new[] { KeyboardModifier.Shift } });
2021-05-15 10:56:10 -07:00
2024-04-29 08:15:12 -07:00
// Ctrl + click or Windows and Linux
// Meta + click on macOS
await page.GetByText("Item").ClickAsync(new() { Modifiers = new[] { KeyboardModifier.ControlOrMeta } });
2021-05-15 10:56:10 -07:00
// Hover over element
2022-10-03 17:02:46 -07:00
await page.GetByText("Item").HoverAsync();
2021-05-15 10:56:10 -07:00
// Click the top left corner
2022-10-03 17:02:46 -07:00
await page.GetByText("Item").ClickAsync(new() { position = new Position { X = 0, Y = 0 } });
2021-05-15 10:56:10 -07:00
```
2020-12-30 18:04:51 -08:00
Under the hood, this and other pointer-related methods:
- wait for element with given selector to be in DOM
- wait for it to become displayed, i.e. not empty, no `display:none` , no `visibility:hidden`
- wait for it to stop moving, for example, until css transition finishes
- scroll the element into view
- wait for it to receive pointer events at the action point, for example, waits until element becomes non-obscured by other elements
- retry if the element is detached during any of the above checks
#### Forcing the click
Sometimes, 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 ](./actionability.md ) checks and force the click:
```js
2022-10-03 17:02:46 -07:00
await page.getByRole('button').click({ force: true });
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
2022-11-30 13:46:33 -08:00
page.getByRole(AriaRole.BUTTON).click(new Locator.ClickOptions().setForce(true));
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2022-10-03 17:02:46 -07:00
await page.get_by_role("button").click(force=True)
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2022-10-03 17:02:46 -07:00
page.get_by_role("button").click(force=True)
2021-01-08 17:39:33 +01:00
```
2021-05-15 10:56:10 -07:00
```csharp
2022-11-30 13:46:33 -08:00
await page.GetByRole(AriaRole.Button).ClickAsync(new() { Force = true });
2021-05-15 10:56:10 -07:00
```
2020-12-30 18:04:51 -08:00
#### Programmatic click
2022-09-09 17:52:06 +02:00
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()` ](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click ) behavior via simply dispatching a click event on the element with [`method: Locator.dispatchEvent` ]:
2020-12-30 18:04:51 -08:00
```js
2022-10-03 17:02:46 -07:00
await page.getByRole('button').dispatchEvent('click');
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
2022-11-30 13:46:33 -08:00
page.getByRole(AriaRole.BUTTON).dispatchEvent("click");
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2022-10-03 17:02:46 -07:00
await page.get_by_role("button").dispatch_event('click')
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2022-10-03 17:02:46 -07:00
page.get_by_role("button").dispatch_event('click')
2021-01-08 17:39:33 +01:00
```
2020-12-30 18:04:51 -08:00
2021-05-15 10:56:10 -07:00
```csharp
2022-11-30 13:46:33 -08:00
await page.GetByRole(AriaRole.Button).DispatchEventAsync("click");
2021-05-15 10:56:10 -07:00
```
2020-12-30 18:04:51 -08:00
## Type characters
2023-09-06 12:41:12 -07:00
:::caution
2023-08-04 14:19:57 -07:00
Most of the time, you should input text with [`method: Locator.fill` ]. See the [Text input ](#text-input ) section above. You only need to type characters if there is special keyboard handling on the page.
:::
2023-08-22 15:21:00 -07:00
Type into the field character by character, as if it was a user with a real keyboard with [`method: Locator.pressSequentially` ].
2020-12-30 18:04:51 -08:00
```js
2023-08-22 15:21:00 -07:00
// Press keys one by one
await page.locator('#area ').pressSequentially('Hello World!');
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
2023-08-22 15:21:00 -07:00
// Press keys one by one
page.locator("#area ").pressSequentially("Hello World!");
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2023-08-22 15:21:00 -07:00
# Press keys one by one
2024-06-17 16:27:36 +02:00
await page.locator('#area ').press_sequentially('Hello World!')
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2023-08-22 15:21:00 -07:00
# Press keys one by one
2024-06-17 16:27:36 +02:00
page.locator('#area ').press_sequentially('Hello World!')
2021-01-08 17:39:33 +01:00
```
2021-05-15 10:56:10 -07:00
```csharp
2023-08-22 15:21:00 -07:00
// Press keys one by one
2024-06-17 16:27:36 +02:00
await Page.Locator("#area ").PressSequentiallyAsync("Hello World!");
2021-05-15 10:56:10 -07:00
```
2020-12-30 18:04:51 -08:00
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
```js
// Hit Enter
2022-10-03 17:02:46 -07:00
await page.getByText('Submit').press('Enter');
2020-12-30 18:04:51 -08:00
// Dispatch Control+Right
2022-10-03 17:02:46 -07:00
await page.getByRole('textbox').press('Control+ArrowRight');
2020-12-30 18:04:51 -08:00
// Press $ sign on keyboard
2022-10-03 17:02:46 -07:00
await page.getByRole('textbox').press('$');
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
// Hit Enter
2022-10-03 17:02:46 -07:00
page.getByText("Submit").press("Enter");
2021-03-01 09:18:44 -08:00
// Dispatch Control+Right
2022-11-30 13:46:33 -08:00
page.getByRole(AriaRole.TEXTBOX).press("Control+ArrowRight");
2021-03-01 09:18:44 -08:00
// Press $ sign on keyboard
2022-11-30 13:46:33 -08:00
page.getByRole(AriaRole.TEXTBOX).press("$");
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Hit Enter
2022-10-03 17:02:46 -07:00
await page.get_by_text("Submit").press("Enter")
2021-01-08 17:39:33 +01:00
# Dispatch Control+Right
2022-10-03 17:02:46 -07:00
await page.get_by_role("textbox").press("Control+ArrowRight")
2021-01-08 17:39:33 +01:00
# Press $ sign on keyboard
2022-10-03 17:02:46 -07:00
await page.get_by_role("textbox").press("$")
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Hit Enter
2022-10-03 17:02:46 -07:00
page.get_by_text("Submit").press("Enter")
2021-01-08 17:39:33 +01:00
# Dispatch Control+Right
2022-10-03 17:02:46 -07:00
page.get_by_role("textbox").press("Control+ArrowRight")
2021-01-08 17:39:33 +01:00
# Press $ sign on keyboard
2022-10-03 17:02:46 -07:00
page.get_by_role("textbox").press("$")
2021-01-08 17:39:33 +01:00
```
2021-05-15 10:56:10 -07:00
```csharp
// Hit Enter
2022-10-03 17:02:46 -07:00
await page.GetByText("Submit").PressAsync("Enter");
2021-05-15 10:56:10 -07:00
// Dispatch Control+Right
2022-11-30 13:46:33 -08:00
await page.GetByRole(AriaRole.Textbox).PressAsync("Control+ArrowRight");
2021-05-15 10:56:10 -07:00
// Press $ sign on keyboard
2022-11-30 13:46:33 -08:00
await page.GetByRole(AriaRole.Textbox).PressAsync("$");
2021-05-15 10:56:10 -07:00
```
2022-09-09 17:52:06 +02:00
The [`method: 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 ](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key ) property of the keyboard events:
2020-12-30 18:04:51 -08:00
2023-06-29 18:26:19 +02:00
```txt
2020-12-30 18:04:51 -08:00
Backquote, Minus, Equal, Backslash, Backspace, Tab, Delete, Escape,
ArrowDown, End, Enter, Home, Insert, PageDown, PageUp, ArrowRight,
ArrowUp, F1 - F12, Digit0 - Digit9, KeyA - KeyZ, etc.
```
- You can alternatively specify a single character you'd like to produce such as `"a"` or `"#"` .
- Following modification shortcuts are also supported: `Shift, Control, Alt, Meta` .
Simple version produces a single character. This character is case-sensitive, so `"a"` and `"A"` will produce different results.
```js
// < input id = name >
2022-07-12 22:39:31 +02:00
await page.locator('#name ').press('Shift+A');
2020-12-30 18:04:51 -08:00
// < input id = name >
2022-07-12 22:39:31 +02:00
await page.locator('#name ').press('Shift+ArrowLeft');
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
// < input id = name >
2022-07-12 22:39:31 +02:00
page.locator("#name ").press("Shift+A");
2021-03-01 09:18:44 -08:00
// < input id = name >
2022-07-12 22:39:31 +02:00
page.locator("#name ").press("Shift+ArrowLeft");
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# <input id=name>
2022-07-12 22:39:31 +02:00
await page.locator('#name ').press('Shift+A')
2021-01-08 17:39:33 +01:00
# <input id=name>
2022-07-12 22:39:31 +02:00
await page.locator('#name ').press('Shift+ArrowLeft')
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# <input id=name>
2022-07-12 22:39:31 +02:00
page.locator('#name ').press('Shift+A')
2021-01-08 17:39:33 +01:00
# <input id=name>
2022-07-12 22:39:31 +02:00
page.locator('#name ').press('Shift+ArrowLeft')
2021-01-08 17:39:33 +01:00
```
2021-05-15 10:56:10 -07:00
```csharp
// < input id = name >
2022-07-12 22:39:31 +02:00
await page.Locator("#name ").PressAsync("Shift+A");
2021-05-15 10:56:10 -07:00
// < input id = name >
2022-07-12 22:39:31 +02:00
await page.Locator("#name ").PressAsync("Shift+ArrowLeft");
2021-05-15 10:56:10 -07:00
```
2020-12-30 18:04:51 -08:00
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
2022-09-07 17:19:54 +02:00
You can select input files for upload using the [`method: Locator.setInputFiles` ] method. It expects first argument to point to an [input element ](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input ) 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.
2021-01-20 08:12:39 -08:00
2020-12-30 18:04:51 -08:00
```js
// Select one file
2023-08-22 17:38:23 +02:00
await page.getByLabel('Upload file').setInputFiles(path.join(__dirname, 'myfile.pdf'));
2020-12-30 18:04:51 -08:00
// Select multiple files
2023-08-22 17:38:23 +02:00
await page.getByLabel('Upload files').setInputFiles([
path.join(__dirname, 'file1.txt'),
path.join(__dirname, 'file2.txt'),
]);
2020-12-30 18:04:51 -08:00
2024-06-13 23:37:47 +02:00
// Select a directory
await page.getByLabel('Upload directory').setInputFiles(path.join(__dirname, 'mydir'));
2020-12-30 18:04:51 -08:00
// Remove all the selected files
2022-10-04 09:29:26 -08:00
await page.getByLabel('Upload file').setInputFiles([]);
2020-12-30 18:04:51 -08:00
// Upload buffer from memory
2022-10-04 09:29:26 -08:00
await page.getByLabel('Upload file').setInputFiles({
2020-12-30 18:04:51 -08:00
name: 'file.txt',
mimeType: 'text/plain',
buffer: Buffer.from('this is test')
});
```
2021-03-01 09:18:44 -08:00
```java
// Select one file
2022-10-04 09:29:26 -08:00
page.getByLabel("Upload file").setInputFiles(Paths.get("myfile.pdf"));
2021-03-01 09:18:44 -08:00
// Select multiple files
2022-10-04 09:29:26 -08:00
page.getByLabel("Upload files").setInputFiles(new Path[] {Paths.get("file1.txt"), Paths.get("file2.txt")});
2021-03-01 09:18:44 -08:00
2024-06-13 23:37:47 +02:00
// Select a directory
page.getByLabel("Upload directory").setInputFiles(Paths.get("mydir"));
2021-03-01 09:18:44 -08:00
// Remove all the selected files
2022-10-04 09:29:26 -08:00
page.getByLabel("Upload file").setInputFiles(new Path[0]);
2021-03-01 09:18:44 -08:00
// Upload buffer from memory
2022-10-04 09:29:26 -08:00
page.getByLabel("Upload file").setInputFiles(new FilePayload(
2021-03-01 09:18:44 -08:00
"file.txt", "text/plain", "this is test".getBytes(StandardCharsets.UTF_8)));
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Select one file
2022-10-04 09:29:26 -08:00
await page.get_by_label("Upload file").set_input_files('myfile.pdf')
2021-01-08 17:39:33 +01:00
# Select multiple files
2022-10-04 09:29:26 -08:00
await page.get_by_label("Upload files").set_input_files(['file1.txt', 'file2.txt'])
2021-01-08 17:39:33 +01:00
2024-06-13 23:37:47 +02:00
# Select a directory
await page.get_by_label("Upload directory").set_input_files('mydir')
2021-01-08 17:39:33 +01:00
# Remove all the selected files
2022-10-04 09:29:26 -08:00
await page.get_by_label("Upload file").set_input_files([])
2021-01-08 17:39:33 +01:00
# Upload buffer from memory
2022-10-04 09:29:26 -08:00
await page.get_by_label("Upload file").set_input_files(
2021-01-14 15:01:39 -08:00
files=[
{"name": "test.txt", "mimeType": "text/plain", "buffer": b"this is a test"}
],
2021-01-08 17:39:33 +01:00
)
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Select one file
2022-10-04 09:29:26 -08:00
page.get_by_label("Upload file").set_input_files('myfile.pdf')
2021-01-08 17:39:33 +01:00
# Select multiple files
2022-10-04 09:29:26 -08:00
page.get_by_label("Upload files").set_input_files(['file1.txt', 'file2.txt'])
2021-01-08 17:39:33 +01:00
2024-06-13 23:37:47 +02:00
# Select a directory
page.get_by_label("Upload directory").set_input_files('mydir')
2021-01-08 17:39:33 +01:00
# Remove all the selected files
2022-10-04 09:29:26 -08:00
page.get_by_label("Upload file").set_input_files([])
2021-01-08 17:39:33 +01:00
# Upload buffer from memory
2022-10-04 09:29:26 -08:00
page.get_by_label("Upload file").set_input_files(
2021-01-14 15:01:39 -08:00
files=[
{"name": "test.txt", "mimeType": "text/plain", "buffer": b"this is a test"}
],
2021-01-08 17:39:33 +01:00
)
```
2021-05-15 10:56:10 -07:00
```csharp
// Select one file
2022-10-04 09:29:26 -08:00
await page.GetByLabel("Upload file").SetInputFilesAsync("myfile.pdf");
2021-05-15 10:56:10 -07:00
// Select multiple files
2022-10-04 09:29:26 -08:00
await page.GetByLabel("Upload files").SetInputFilesAsync(new[] { "file1.txt", "file12.txt" });
2021-05-15 10:56:10 -07:00
2024-06-13 23:37:47 +02:00
// Select a directory
await page.GetByLabel("Upload directory").SetInputFilesAsync("mydir");
2021-05-15 10:56:10 -07:00
// Remove all the selected files
2022-10-04 09:29:26 -08:00
await page.GetByLabel("Upload file").SetInputFilesAsync(new[] {});
2021-05-15 10:56:10 -07:00
// Upload buffer from memory
2022-10-04 09:29:26 -08:00
await page.GetByLabel("Upload file").SetInputFilesAsync(new FilePayload
2021-05-15 10:56:10 -07:00
{
Name = "file.txt",
MimeType = "text/plain",
2021-06-22 13:56:28 +01:00
Buffer = System.Text.Encoding.UTF8.GetBytes("this is a test"),
2021-05-15 10:56:10 -07:00
});
```
2021-02-04 19:34:09 +01:00
If you don't have input element in hand (it is created dynamically), you can handle the [`event: Page.fileChooser` ] event
2021-01-20 08:12:39 -08:00
or use a corresponding waiting method upon your action:
2020-12-30 18:04:51 -08:00
2021-01-20 08:12:39 -08:00
```js
2022-11-30 12:36:35 -08:00
// Start waiting for file chooser before clicking. Note no await.
const fileChooserPromise = page.waitForEvent('filechooser');
await page.getByLabel('Upload file').click();
const fileChooser = await fileChooserPromise;
2023-08-22 17:38:23 +02:00
await fileChooser.setFiles(path.join(__dirname, 'myfile.pdf'));
2021-01-20 08:12:39 -08:00
```
2020-12-30 18:04:51 -08:00
2021-03-01 09:18:44 -08:00
```java
FileChooser fileChooser = page.waitForFileChooser(() -> {
2022-11-30 12:36:35 -08:00
page.getByLabel("Upload file").click();
2021-03-01 09:18:44 -08:00
});
fileChooser.setFiles(Paths.get("myfile.pdf"));
```
2021-01-20 08:12:39 -08:00
```python async
async with page.expect_file_chooser() as fc_info:
2022-11-30 12:36:35 -08:00
await page.get_by_label("Upload file").click()
2021-01-20 08:12:39 -08:00
file_chooser = await fc_info.value
await file_chooser.set_files("myfile.pdf")
```
2020-12-30 18:04:51 -08:00
2021-01-20 08:12:39 -08:00
```python sync
with page.expect_file_chooser() as fc_info:
2022-11-30 12:36:35 -08:00
page.get_by_label("Upload file").click()
2021-01-20 08:12:39 -08:00
file_chooser = fc_info.value
file_chooser.set_files("myfile.pdf")
```
2020-12-30 18:04:51 -08:00
2021-05-15 10:56:10 -07:00
```csharp
2021-05-26 15:11:31 -07:00
var fileChooser = page.RunAndWaitForFileChooserAsync(async () =>
2021-05-19 17:19:25 -07:00
{
2022-11-30 12:36:35 -08:00
await page.GetByLabel("Upload file").ClickAsync();
2021-05-19 17:19:25 -07:00
});
2021-05-15 14:02:07 -07:00
await fileChooser.SetFilesAsync("myfile.pdf");
2021-05-15 10:56:10 -07:00
```
2020-12-30 18:04:51 -08:00
## Focus element
2022-09-07 17:19:54 +02:00
For the dynamic pages that handle focus events, you can focus the given element with [`method: Locator.focus` ].
2020-12-30 18:04:51 -08:00
```js
2022-10-04 09:29:26 -08:00
await page.getByLabel('Password').focus();
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
2022-10-04 09:29:26 -08:00
page.getByLabel("Password").focus();
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2022-10-04 09:29:26 -08:00
await page.get_by_label('password').focus()
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2022-10-04 09:29:26 -08:00
page.get_by_label('password').focus()
2021-01-08 17:39:33 +01:00
```
2021-05-15 10:56:10 -07:00
```csharp
2022-10-04 09:29:26 -08:00
await page.GetByLabel("Password").FocusAsync();
2021-05-15 10:56:10 -07:00
```
2022-09-28 14:36:27 -07:00
## Drag and Drop
You can perform drag& drop operation with [`method: 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.
```js
await page.locator('#item -to-be-dragged').dragTo(page.locator('#item -to-drop-at'));
```
```java
page.locator("#item -to-be-dragged").dragTo(page.locator("#item -to-drop-at"));
```
```python async
await page.locator("#item -to-be-dragged").drag_to(page.locator("#item -to-drop-at"))
```
```python sync
page.locator("#item -to-be-dragged").drag_to(page.locator("#item -to-drop-at"))
```
```csharp
await page.Locator("#item -to-be-dragged").DragToAsync(page.Locator("#item -to-drop-at"));
```
### Dragging manually
If you want precise control over the drag operation, use lower-level methods like [`method: Locator.hover` ], [`method: Mouse.down` ], [`method: Mouse.move` ] and [`method: Mouse.up` ].
```js
await page.locator('#item -to-be-dragged').hover();
await page.mouse.down();
await page.locator('#item -to-drop-at').hover();
await page.mouse.up();
```
```java
page.locator("#item -to-be-dragged").hover();
2023-01-23 15:25:00 +05:30
page.mouse().down();
2022-09-28 14:36:27 -07:00
page.locator("#item -to-drop-at").hover();
2023-01-23 15:25:00 +05:30
page.mouse().up();
2022-09-28 14:36:27 -07:00
```
```python async
await page.locator("#item -to-be-dragged").hover()
await page.mouse.down()
await page.locator("#item -to-drop-at").hover()
await page.mouse.up()
```
```python sync
page.locator("#item -to-be-dragged").hover()
page.mouse.down()
page.locator("#item -to-drop-at").hover()
page.mouse.up()
```
```csharp
await page.Locator("#item -to-be-dragged").HoverAsync();
await page.Mouse.DownAsync();
await page.Locator("#item -to-drop-at").HoverAsync();
await page.Mouse.UpAsync();
```
:::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 [`method: Mouse.move` ] or [`method: 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.
:::
2024-05-08 21:04:05 -07:00
## Scrolling
Most of the time, Playwright will automatically scroll for you before doing any actions. Therefore, you do not need to scroll explicitly.
```js
// Scrolls automatically so that button is visible
await page.getByRole('button').click();
```
```java
// Scrolls automatically so that button is visible
page.getByRole(AriaRole.BUTTON).click();
```
```python async
# Scrolls automatically so that button is visible
await page.get_by_role("button").click()
```
```python sync
# Scrolls automatically so that button is visible
page.get_by_role("button").click()
```
```csharp
// Scrolls automatically so that button is visible
await page.GetByRole(AriaRole.Button).ClickAsync();
```
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.
```js
// Scroll the footer into view, forcing an "infinite list" to load more content
await page.getByText('Footer text').scrollIntoViewIfNeeded();
```
```java
// Scroll the footer into view, forcing an "infinite list" to load more content
page.getByText("Footer text").scrollIntoViewIfNeeded();
```
```python async
# Scroll the footer into view, forcing an "infinite list" to load more content
await page.get_by_text("Footer text").scroll_into_view_if_needed()
```
```python sync
# Scroll the footer into view, forcing an "infinite list" to load more content
page.get_by_text("Footer text").scroll_into_view_if_needed()
```
```csharp
// Scroll the footer into view, forcing an "infinite list" to load more content
await page.GetByText("Footer text").ScrollIntoViewIfNeededAsync();
```
If you would like to control the scrolling more precisely, use [`method: Mouse.wheel` ] or [`method: Locator.evaluate` ]:
```js
// Position the mouse and scroll with the mouse wheel
await page.getByTestId('scrolling-container').hover();
await page.mouse.wheel(0, 10);
// Alternatively, programmatically scroll a specific element
await page.getByTestId('scrolling-container').evaluate(e => e.scrollTop += 100);
```
```java
// Position the mouse and scroll with the mouse wheel
page.getByTestId("scrolling-container").hover();
page.mouse.wheel(0, 10);
// Alternatively, programmatically scroll a specific element
page.getByTestId("scrolling-container").evaluate("e => e.scrollTop += 100");
```
```python async
# Position the mouse and scroll with the mouse wheel
await page.get_by_test_id("scrolling-container").hover()
await page.mouse.wheel(0, 10)
# Alternatively, programmatically scroll a specific element
await page.get_by_test_id("scrolling-container").evaluate("e => e.scrollTop += 100")
```
```python sync
# Position the mouse and scroll with the mouse wheel
page.get_by_test_id("scrolling-container").hover()
page.mouse.wheel(0, 10)
# Alternatively, programmatically scroll a specific element
page.get_by_test_id("scrolling-container").evaluate("e => e.scrollTop += 100")
```
```csharp
// Position the mouse and scroll with the mouse wheel
await page.GetByTestId("scrolling-container").HoverAsync();
await page.Mouse.WheelAsync(0, 10);
// Alternatively, programmatically scroll a specific element
await page.GetByTestId("scrolling-container").EvaluateAsync("e => e.scrollTop += 100");
```