2021-01-01 15:17:27 -08:00
---
id: input
title: "Input"
---
2020-12-30 18:04:51 -08:00
2021-01-01 15:17:27 -08:00
<!-- TOC -->
2020-12-30 18:04:51 -08:00
## Text input
This 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>` , `[contenteditable]` and `<label>` associated with an input or textarea.
```js
// Text input
2022-07-12 22:39:31 +02:00
await page.locator('#name ').fill('Peter');
2020-12-30 18:04:51 -08:00
// Date input
2022-07-12 22:39:31 +02:00
await page.locator('#date ').fill('2020-02-02');
2020-12-30 18:04:51 -08:00
// Time input
2022-07-12 22:39:31 +02:00
await page.locator('#time ').fill('13:15');
2020-12-30 18:04:51 -08:00
// Local datetime input
2022-07-12 22:39:31 +02:00
await page.locator('#local ').fill('2020-03-02T05:15');
2020-12-30 18:04:51 -08:00
// Input through label
2022-07-12 22:39:31 +02:00
await page.locator('text=First Name').fill('Peter');
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
// Text input
2022-07-12 22:39:31 +02:00
page.locator("#name ").fill("Peter");
2021-03-01 09:18:44 -08:00
// Date input
2022-07-12 22:39:31 +02:00
page.locator("#date ").fill("2020-02-02");
2021-03-01 09:18:44 -08:00
// Time input
2022-07-12 22:39:31 +02:00
page.locator("#time ").fill("13-15");
2021-03-01 09:18:44 -08:00
// Local datetime input
2022-07-12 22:39:31 +02:00
page.locator("#local ").fill("2020-03-02T05:15");
2021-03-01 09:18:44 -08:00
// Input through label
2022-07-12 22:39:31 +02:00
page.locator("text=First Name").fill("Peter");
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-07-12 22:39:31 +02:00
await page.locator('#name ').fill('Peter')
2021-01-08 17:39:33 +01:00
# Date input
2022-07-12 22:39:31 +02:00
await page.locator('#date ').fill('2020-02-02')
2021-01-08 17:39:33 +01:00
# Time input
2022-07-12 22:39:31 +02:00
await page.locator('#time ').fill('13:15')
2021-01-08 17:39:33 +01:00
# Local datetime input
2022-07-12 22:39:31 +02:00
await page.locator('#local ').fill('2020-03-02T05:15')
2021-01-08 17:39:33 +01:00
# Input through label
2022-07-12 22:39:31 +02:00
await page.locator('text=First Name').fill('Peter')
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-07-12 22:39:31 +02:00
page.locator('#name ').fill('Peter')
2021-01-08 17:39:33 +01:00
# Date input
2022-07-12 22:39:31 +02:00
page.locator('#date ').fill('2020-02-02')
2021-01-08 17:39:33 +01:00
# Time input
2022-07-12 22:39:31 +02:00
page.locator('#time ').fill('13:15')
2021-01-08 17:39:33 +01:00
# Local datetime input
2022-07-12 22:39:31 +02:00
page.locator('#local ').fill('2020-03-02T05:15')
2021-01-08 17:39:33 +01:00
# Input through label
2022-07-12 22:39:31 +02:00
page.locator('text=First Name').fill('Peter')
2021-01-08 17:39:33 +01:00
```
2021-05-15 10:56:10 -07:00
```csharp
// Text input
2022-07-12 22:39:31 +02:00
await page.Locator("#name ").FillAsync("Peter");
2021-05-15 10:56:10 -07:00
// Date input
2022-07-12 22:39:31 +02:00
await page.Locator("#date ").FillAsync("2020-02-02");
2021-05-15 10:56:10 -07:00
// Time input
2022-07-12 22:39:31 +02:00
await page.Locator("#time ").FillAsync("13-15");
2021-05-15 10:56:10 -07:00
// Local datetime input
2022-07-12 22:39:31 +02:00
await page.Locator("#local ").FillAsync("2020-03-02T05:15");
2021-05-15 10:56:10 -07:00
// Input through label
2022-07-12 22:39:31 +02:00
await page.Locator("text=First Name").FillAsync("Peter");
2021-05-15 10:56:10 -07:00
```
2021-01-17 21:09:40 -08:00
### API reference
2020-12-30 18:04:51 -08:00
2022-07-12 22:39:31 +02:00
- [`method: Locator.fill` ]
2020-12-30 18:04:51 -08:00
- [`method: Page.fill` ]
- [`method: Frame.fill` ]
< br / >
## Checkboxes and radio buttons
This 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]` , `[role=checkbox]` or `label` associated with checkbox or radio button.
```js
// Check the checkbox
2022-07-12 22:39:31 +02:00
await page.locator('#agree ').check();
2020-12-30 18:04:51 -08:00
2021-01-17 21:09:40 -08:00
// Assert the checked state
2022-07-12 22:39:31 +02:00
expect(await page.locator('#agree ').isChecked()).toBeTruthy()
2021-01-17 21:09:40 -08:00
2020-12-30 18:04:51 -08:00
// Uncheck by input < label > .
2022-07-12 22:39:31 +02:00
await page.locator('#subscribe -label').uncheck();
2020-12-30 18:04:51 -08:00
// Select the radio button
2022-07-12 22:39:31 +02:00
await page.locator('text=XL').check();
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
// Check the checkbox
2022-07-12 22:39:31 +02:00
page.locator("#agree ").check();
2021-03-01 09:18:44 -08:00
// Assert the checked state
2022-07-12 22:39:31 +02:00
assertTrue(page.locator("#agree ").isChecked());
2021-03-01 09:18:44 -08:00
// Uncheck by input < label > .
2022-07-12 22:39:31 +02:00
page.locator("#subscribe -label").uncheck();
2021-03-01 09:18:44 -08:00
// Select the radio button
2022-07-12 22:39:31 +02:00
page.locator("text=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-07-12 22:39:31 +02:00
await page.locator('#agree ').check()
2021-01-08 17:39:33 +01:00
2021-01-17 21:09:40 -08:00
# Assert the checked state
2022-07-12 22:39:31 +02:00
assert await page.locator('#agree ').is_checked() is True
2021-01-17 21:09:40 -08:00
2021-01-08 17:39:33 +01:00
# Uncheck by input <label>.
2022-07-12 22:39:31 +02:00
await page.locator('#subscribe -label').uncheck()
2021-01-08 17:39:33 +01:00
# Select the radio button
2022-07-12 22:39:31 +02:00
await page.locator('text=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-07-12 22:39:31 +02:00
page.locator('#agree ').check()
2021-01-08 17:39:33 +01:00
2021-01-17 21:09:40 -08:00
# Assert the checked state
2022-07-12 22:39:31 +02:00
assert page.locator('#agree ').is_checked() is True
2021-01-17 21:09:40 -08:00
2021-01-08 17:39:33 +01:00
# Uncheck by input <label>.
2022-07-12 22:39:31 +02:00
page.locator('#subscribe -label').uncheck()
2021-01-08 17:39:33 +01:00
# Select the radio button
2022-07-12 22:39:31 +02:00
page.locator('text=XL').check()
2021-01-08 17:39:33 +01:00
```
2021-05-15 10:56:10 -07:00
```csharp
// Check the checkbox
2022-07-12 22:39:31 +02:00
await page.Locator("#agree ").CheckAsync();
2021-05-15 10:56:10 -07:00
// Assert the checked state
2022-07-12 22:39:31 +02:00
Assert.True(await page.Locator("#agree ").IsCheckedAsync());
2021-05-15 10:56:10 -07:00
// Uncheck by input < label > .
2022-07-12 22:39:31 +02:00
await page.Locator("#subscribe -label").UncheckAsync();
2021-05-15 10:56:10 -07:00
// Select the radio button
2022-07-12 22:39:31 +02:00
await page.Locator("text=XL").CheckAsync();
2021-05-15 10:56:10 -07:00
```
2021-01-17 21:09:40 -08:00
### API reference
2020-12-30 18:04:51 -08:00
2022-04-14 22:24:06 +02:00
- [`method: Locator.check` ]
- [`method: Locator.isChecked` ]
- [`method: Locator.uncheck` ]
2022-07-12 22:39:31 +02:00
- [`method: Page.check` ]
- [`method: Page.isChecked` ]
- [`method: Page.uncheck` ]
2020-12-30 18:04:51 -08:00
< br / >
## Select options
Selects one or multiple options in the `<select>` element.
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
// Single selection matching the value
2022-07-12 22:39:31 +02:00
await page.locator('select#colors ').selectOption('blue');
2020-12-30 18:04:51 -08:00
// Single selection matching the label
2022-07-12 22:39:31 +02:00
await page.locator('select#colors ').selectOption({ label: 'Blue' });
2020-12-30 18:04:51 -08:00
// Multiple selected items
2022-07-12 22:39:31 +02:00
await page.locator('select#colors ').selectOption(['red', 'green', 'blue']);
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
// Single selection matching the value
2022-07-12 22:39:31 +02:00
page.locator("select#colors ").selectOption("blue");
2021-03-01 09:18:44 -08:00
// Single selection matching the label
2022-07-12 22:39:31 +02:00
page.locator("select#colors ").selectOption(new SelectOption().setLabel("Blue"));
2021-03-01 09:18:44 -08:00
// Multiple selected items
2022-07-12 22:39:31 +02:00
page.locator("select#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
2021-01-08 17:39:33 +01:00
# Single selection matching the value
2022-07-12 22:39:31 +02:00
await page.locator('select#colors ').select_option('blue')
2021-01-08 17:39:33 +01:00
# Single selection matching the label
2022-07-12 22:39:31 +02:00
await page.locator('select#colors ').select_option(label='Blue')
2021-01-08 17:39:33 +01:00
# Multiple selected items
2022-07-12 22:39:31 +02:00
await page.locator('select#colors ').select_option(['red', 'green', 'blue'])
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
# Single selection matching the value
2022-07-12 22:39:31 +02:00
page.locator('select#colors ').select_option('blue')
2021-01-08 17:39:33 +01:00
# Single selection matching the label
2022-07-12 22:39:31 +02:00
page.locator('select#colors ').select_option(label='Blue')
2021-01-08 17:39:33 +01:00
# Multiple selected items
2022-07-12 22:39:31 +02:00
page.locator('select#colors ').select_option(['red', 'green', 'blue'])
2021-01-08 17:39:33 +01:00
```
2021-05-15 10:56:10 -07:00
```csharp
// Single selection matching the value
2022-07-12 22:39:31 +02:00
await page.Locator("select#colors ").SelectOptionAsync("blue");
2021-05-15 10:56:10 -07:00
// Single selection matching the label
2022-07-12 22:39:31 +02:00
await page.Locator("select#colors ").SelectOptionAsync(new SelectOptionValue { Label = "blue" }));
2021-05-15 10:56:10 -07:00
// Multiple selected items
2022-07-12 22:39:31 +02:00
await page.Locator("select#colors ").SelectOptionAsync(new[] { "blue", "green", "red" });
2021-05-15 10:56:10 -07:00
```
2021-01-17 21:09:40 -08:00
### API reference
2020-12-30 18:04:51 -08:00
2022-07-12 22:39:31 +02:00
- [`method: Locator.selectOption` ]
2020-12-30 18:04:51 -08:00
- [`method: Page.selectOption` ]
- [`method: Frame.selectOption` ]
< br / >
## Mouse click
Performs a simple human click.
```js
// Generic click
2022-07-12 22:39:31 +02:00
await page.locator('button#submit ').click();
2020-12-30 18:04:51 -08:00
// Double click
2022-07-12 22:39:31 +02:00
await page.locator('#item ').dblclick();
2020-12-30 18:04:51 -08:00
// Right click
2022-07-12 22:39:31 +02:00
await page.locator('#item ').click({ button: 'right' });
2020-12-30 18:04:51 -08:00
// Shift + click
2022-07-12 22:39:31 +02:00
await page.locator('#item ').click({ modifiers: ['Shift'] });
2020-12-30 18:04:51 -08:00
// Hover over element
2022-07-12 22:39:31 +02:00
await page.locator('#item ').hover();
2020-12-30 18:04:51 -08:00
// Click the top left corner
2022-07-12 22:39:31 +02:00
await page.locator('#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-07-12 22:39:31 +02:00
page.locator("button#submit ").click();
2021-03-01 09:18:44 -08:00
// Double click
2022-07-12 22:39:31 +02:00
page.locator("#item ").dblclick();
2021-03-01 09:18:44 -08:00
// Right click
2022-07-12 22:39:31 +02:00
page.locator("#item ").click(new Locator.ClickOptions().setButton(MouseButton.RIGHT));
2021-03-01 09:18:44 -08:00
// Shift + click
2022-07-12 22:39:31 +02:00
page.locator("#item ").click(new Locator.ClickOptions().setModifiers(Arrays.asList(KeyboardModifier.SHIFT)));
2021-03-01 09:18:44 -08:00
// Hover over element
2022-07-12 22:39:31 +02:00
page.locator("#item ").hover();
2021-03-01 09:18:44 -08:00
// Click the top left corner
2022-07-12 22:39:31 +02:00
page.locator("#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-07-12 22:39:31 +02:00
await page.locator('button#submit ').click()
2021-01-08 17:39:33 +01:00
# Double click
2022-07-12 22:39:31 +02:00
await page.locator('#item ').dblclick()
2021-01-08 17:39:33 +01:00
# Right click
2022-07-12 22:39:31 +02:00
await page.locator('#item ').click(button='right')
2021-01-08 17:39:33 +01:00
# Shift + click
2022-07-12 22:39:31 +02:00
await page.locator('#item ').click(modifiers=['Shift'])
2021-01-08 17:39:33 +01:00
# Hover over element
2022-07-12 22:39:31 +02:00
await page.locator('#item ').hover()
2021-01-08 17:39:33 +01:00
# Click the top left corner
2022-07-12 22:39:31 +02:00
await page.locator('#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-07-12 22:39:31 +02:00
page.locator('button#submit ').click()
2021-01-08 17:39:33 +01:00
# Double click
2022-07-12 22:39:31 +02:00
page.locator('#item ').dblclick()
2021-01-08 17:39:33 +01:00
# Right click
2022-07-12 22:39:31 +02:00
page.locator('#item ').click(button='right')
2021-01-08 17:39:33 +01:00
# Shift + click
2022-07-12 22:39:31 +02:00
page.locator('#item ').click(modifiers=['Shift'])
2021-01-08 17:39:33 +01:00
# Hover over element
2022-07-12 22:39:31 +02:00
page.locator('#item ').hover()
2021-01-08 17:39:33 +01:00
# Click the top left corner
2022-07-12 22:39:31 +02:00
page.locator('#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-07-12 22:39:31 +02:00
await page.Locator("button#submit ").ClickAsync();
2021-05-15 10:56:10 -07:00
// Double click
2022-07-12 22:39:31 +02:00
await page.Locator("#item ").DblClickAsync();
2021-05-15 10:56:10 -07:00
// Right click
2022-07-12 22:39:31 +02:00
await page.Locator("#item ").ClickAsync(new() { Button = MouseButton.Right });
2021-05-15 10:56:10 -07:00
// Shift + click
2022-07-12 22:39:31 +02:00
await page.Locator("#item ").ClickAsync(new() { Modifiers = new[] { KeyboardModifier.Shift } });
2021-05-15 10:56:10 -07:00
// Hover over element
2022-07-12 22:39:31 +02:00
await page.Locator("#item ").HoverAsync();
2021-05-15 10:56:10 -07:00
// Click the top left corner
2022-07-12 22:39:31 +02:00
await page.Locator("#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-07-12 22:39:31 +02:00
await page.locator('button#submit ').click({ force: true });
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
2022-07-12 22:39:31 +02:00
page.locator("button#submit ").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-07-12 22:39:31 +02:00
await page.locator('button#submit ').click(force=True)
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2022-07-12 22:39:31 +02:00
page.locator('button#submit ').click(force=True)
2021-01-08 17:39:33 +01:00
```
2021-05-15 10:56:10 -07:00
```csharp
2022-07-12 22:39:31 +02:00
await page.Locator("button#submit ").ClickAsync(new() { Force = true });
2021-05-15 10:56:10 -07:00
```
2020-12-30 18:04:51 -08:00
#### Programmatic click
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:
```js
2022-07-12 22:39:31 +02:00
await page.locator('button#submit ').dispatchEvent('click');
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
2022-07-12 22:39:31 +02:00
page.locator("button#submit ").dispatchEvent("click");
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2022-07-12 22:39:31 +02:00
await page.locator('button#submit ').dispatch_event('click')
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2022-07-12 22:39:31 +02:00
page.locator('button#submit ').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-07-12 22:39:31 +02:00
await page.Locator("button#submit ").DispatchEventAsync("click");
2021-05-15 10:56:10 -07:00
```
2021-01-17 21:09:40 -08:00
### API reference
2020-12-30 18:04:51 -08:00
2022-07-12 22:39:31 +02:00
- [`method: Locator.click` ]
2020-12-30 18:04:51 -08:00
- [`method: Page.click` ]
- [`method: Frame.click` ]
2022-07-12 22:39:31 +02:00
- [`method: Locator.dblclick` ]
2020-12-30 18:04:51 -08:00
- [`method: Page.dblclick` ]
- [`method: Frame.dblclick` ]
2022-07-12 22:39:31 +02:00
- [`method: Locator.hover` ]
2020-12-30 18:04:51 -08:00
- [`method: Page.hover` ]
- [`method: Frame.hover` ]
2022-07-12 22:39:31 +02:00
- [`method: Locator.dispatchEvent` ]
2020-12-30 18:04:51 -08:00
- [`method: Page.dispatchEvent` ]
- [`method: Frame.dispatchEvent` ]
< br / >
## Type characters
Type into the field character by character, as if it was a user with a real keyboard.
```js
// Type character by character
2022-07-12 22:39:31 +02:00
await page.locator('#area ').type('Hello World!');
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
// Type character by character
2022-07-12 22:39:31 +02:00
page.locator("#area ").type("Hello World!");
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
# Type character by character
2022-07-12 22:39:31 +02:00
await page.locator('#area ').type('Hello World!')
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
# Type character by character
2022-07-12 22:39:31 +02:00
page.locator('#area ').type('Hello World!')
2021-01-08 17:39:33 +01:00
```
2021-05-15 10:56:10 -07:00
```csharp
// Type character by character
2022-07-12 22:39:31 +02:00
await page.Locator("#area ").TypeAsync("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.
2021-01-12 12:14:27 -08:00
:::note
Most of the time, [`method: Page.fill` ] will just work. You only need to type characters if there is special keyboard handling on the page.
:::
2020-12-30 18:04:51 -08:00
2021-01-17 21:09:40 -08:00
### API reference
2020-12-30 18:04:51 -08:00
2022-07-12 22:39:31 +02:00
- [`method: Locator.type` ]
2020-12-30 18:04:51 -08:00
- [`method: Page.type` ]
- [`method: Frame.type` ]
- [`method: Keyboard.type` ]
< br / >
## Keys and shortcuts
```js
// Hit Enter
2022-07-12 22:39:31 +02:00
await page.locator('#submit ').press('Enter');
2020-12-30 18:04:51 -08:00
// Dispatch Control+Right
2022-07-12 22:39:31 +02:00
await page.locator('#name ').press('Control+ArrowRight');
2020-12-30 18:04:51 -08:00
// Press $ sign on keyboard
2022-07-12 22:39:31 +02:00
await page.locator('#value ').press('$');
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
// Hit Enter
2022-07-12 22:39:31 +02:00
page.locator("#submit ").press("Enter");
2021-03-01 09:18:44 -08:00
// Dispatch Control+Right
2022-07-12 22:39:31 +02:00
page.locator("#name ").press("Control+ArrowRight");
2021-03-01 09:18:44 -08:00
// Press $ sign on keyboard
2022-07-12 22:39:31 +02:00
page.locator("#value ").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-07-12 22:39:31 +02:00
await page.locator('#submit ').press('Enter')
2021-01-08 17:39:33 +01:00
# Dispatch Control+Right
2022-07-12 22:39:31 +02:00
await page.locator('#name ').press('Control+ArrowRight')
2021-01-08 17:39:33 +01:00
# Press $ sign on keyboard
2022-07-12 22:39:31 +02:00
await page.locator('#value ').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-07-12 22:39:31 +02:00
page.locator('#submit ').press('Enter')
2021-01-08 17:39:33 +01:00
# Dispatch Control+Right
2022-07-12 22:39:31 +02:00
page.locator('#name ').press('Control+ArrowRight')
2021-01-08 17:39:33 +01:00
# Press $ sign on keyboard
2022-07-12 22:39:31 +02:00
page.locator('#value ').press('$')
2021-01-08 17:39:33 +01:00
```
2021-05-15 10:56:10 -07:00
```csharp
// Hit Enter
2022-07-12 22:39:31 +02:00
await page.Locator("#submit ").PressAsync("Enter");
2021-05-15 10:56:10 -07:00
// Dispatch Control+Right
2022-07-12 22:39:31 +02:00
await page.Locator("#name ").PressAsync("Control+ArrowRight");
2021-05-15 10:56:10 -07:00
// Press $ sign on keyboard
2022-07-12 22:39:31 +02:00
await page.Locator("#value ").PressAsync("$");
2021-05-15 10:56:10 -07:00
```
2020-12-30 18:04:51 -08:00
This 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:
```
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.
2021-01-17 21:09:40 -08:00
### API reference
2020-12-30 18:04:51 -08:00
2022-07-12 22:39:31 +02:00
- [`method: Locator.press` ]
2020-12-30 18:04:51 -08:00
- [`method: Page.press` ]
- [`method: Frame.press` ]
- [`method: Keyboard.press` ]
< br / >
## Upload files
2021-01-20 08:12:39 -08:00
You can select input files for upload using the [`method: Page.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.
2020-12-30 18:04:51 -08:00
```js
// Select one file
2022-07-12 22:39:31 +02:00
await page.locator('input#upload ').setInputFiles('myfile.pdf');
2020-12-30 18:04:51 -08:00
// Select multiple files
2022-07-12 22:39:31 +02:00
await page.locator('input#upload ').setInputFiles(['file1.txt', 'file2.txt']);
2020-12-30 18:04:51 -08:00
// Remove all the selected files
2022-07-12 22:39:31 +02:00
await page.locator('input#upload ').setInputFiles([]);
2020-12-30 18:04:51 -08:00
// Upload buffer from memory
2022-07-12 22:39:31 +02:00
await page.locator('input#upload ').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-07-12 22:39:31 +02:00
page.locator("input#upload ").setInputFiles(Paths.get("myfile.pdf"));
2021-03-01 09:18:44 -08:00
// Select multiple files
2022-07-12 22:39:31 +02:00
page.locator("input#upload ").setInputFiles(new Path[] {Paths.get("file1.txt"), Paths.get("file2.txt")});
2021-03-01 09:18:44 -08:00
// Remove all the selected files
2022-07-12 22:39:31 +02:00
page.locator("input#upload ").setInputFiles(new Path[0]);
2021-03-01 09:18:44 -08:00
// Upload buffer from memory
2022-07-12 22:39:31 +02:00
page.locator("input#upload ").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-07-12 22:39:31 +02:00
await page.locator('input#upload ').set_input_files('myfile.pdf')
2021-01-08 17:39:33 +01:00
# Select multiple files
2022-07-12 22:39:31 +02:00
await page.locator('input#upload ').set_input_files(['file1.txt', 'file2.txt'])
2021-01-08 17:39:33 +01:00
# Remove all the selected files
2022-07-12 22:39:31 +02:00
await page.locator('input#upload ').set_input_files([])
2021-01-08 17:39:33 +01:00
# Upload buffer from memory
2022-07-12 22:39:31 +02:00
await page.locator("input#upload ").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-07-12 22:39:31 +02:00
page.locator('input#upload ').set_input_files('myfile.pdf')
2021-01-08 17:39:33 +01:00
# Select multiple files
2022-07-12 22:39:31 +02:00
page.locator('input#upload ').set_input_files(['file1.txt', 'file2.txt'])
2021-01-08 17:39:33 +01:00
# Remove all the selected files
2022-07-12 22:39:31 +02:00
page.locator('input#upload ').set_input_files([])
2021-01-08 17:39:33 +01:00
# Upload buffer from memory
2022-07-12 22:39:31 +02:00
page.locator("input#upload ").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-07-12 22:39:31 +02:00
await page.Locator("input#upload ").SetInputFilesAsync("myfile.pdf");
2021-05-15 10:56:10 -07:00
// Select multiple files
2022-07-12 22:39:31 +02:00
await page.Locator("input#upload ").SetInputFilesAsync(new[] { "file1.txt", "file12.txt" });
2021-05-15 10:56:10 -07:00
// Remove all the selected files
2022-07-12 22:39:31 +02:00
await page.Locator("input#upload ").SetInputFilesAsync(new[] {});
2021-05-15 10:56:10 -07:00
// Upload buffer from memory
2022-07-12 22:39:31 +02:00
await page.Locator("input#upload ").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-01-13 10:38:22 -08:00
// Note that Promise.all prevents a race condition
// between clicking and waiting for the file chooser.
2021-01-20 08:12:39 -08:00
const [fileChooser] = await Promise.all([
2022-01-13 10:38:22 -08:00
// It is important to call waitForEvent before click to set up waiting.
2021-01-20 08:12:39 -08:00
page.waitForEvent('filechooser'),
2022-01-13 10:38:22 -08:00
page.locator('upload').click(),
2021-01-20 08:12:39 -08:00
]);
await fileChooser.setFiles('myfile.pdf');
```
2020-12-30 18:04:51 -08:00
2021-03-01 09:18:44 -08:00
```java
FileChooser fileChooser = page.waitForFileChooser(() -> {
2022-07-12 22:39:31 +02:00
page.locator("upload").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-07-12 22:39:31 +02:00
await page.locator("upload").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-07-12 22:39:31 +02:00
page.locator("upload").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-07-12 22:39:31 +02:00
await page.Locator("upload").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
```
2021-01-20 08:12:39 -08:00
### API reference
- [FileChooser]
2022-07-12 22:39:31 +02:00
- [`method: Locator.setInputFiles` ]
2020-12-30 18:04:51 -08:00
- [`method: Page.setInputFiles` ]
- [`method: Frame.setInputFiles` ]
< br / >
## Focus element
For the dynamic pages that handle focus events, you can focus the given element.
```js
2022-07-12 22:39:31 +02:00
await page.locator('input#name ').focus();
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
2022-07-12 22:39:31 +02:00
page.locator("input#name ").focus();
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2022-07-12 22:39:31 +02:00
await page.locator('input#name ').focus()
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2022-07-12 22:39:31 +02:00
page.locator('input#name ').focus()
2021-01-08 17:39:33 +01:00
```
2021-05-15 10:56:10 -07:00
```csharp
2022-07-12 22:39:31 +02:00
await page.Locator("input#name ").FocusAsync();
2021-05-15 10:56:10 -07:00
```
2021-01-17 21:09:40 -08:00
### API reference
2020-12-30 18:04:51 -08:00
2022-07-12 22:39:31 +02:00
- [`method: Locator.focus` ]
2020-12-30 18:04:51 -08:00
- [`method: Page.focus` ]
- [`method: Frame.focus` ]
< br / >