Playwright Actions
This chapter introduces actions related to navigation and clicking in Playwright, as well as the auto-waiting mechanism behind them.
What is an Action
An Action is a method in Playwright used to simulate user behavior, such as clicking a button, entering text, or selecting a dropdown.
Before each Action is executed, Playwright automatically waits for the target element to become actionable.
Actionability checks
Before executing each action, Playwright performs a series of checks on the target element:
| Step | Check condition | Description |
|---|---|---|
| 1. Attached | The element is attached to the DOM tree | The element is present in the page |
| 2. Visible | The element is visible (not display:none or visibility:hidden) |
The user can see the element |
| 3. Stable | The element is stable (animation finished) | Avoids misoperations during animation |
| 4. Receives Events | The element is not obscured by other elements | Ensures the click can be received by the element |
| 5. Enabled | The element is not disabled (not disabled) |
The user can interact with it |
If any step fails, Playwright waits and retries until a timeout occurs.
These 5 checks are performed automatically by Playwright; you do not need to write any extra waiting code. This is also the fundamental reason Playwright can greatly reduce flaky tests.
Forced action { force: true }
Sometimes you need to bypass actionability checks, for example clicking a button hidden by an overlay or testing the state of an invisible element.
Example
// Force click, skipping all actionability checks
await page.
getByRole
(
'button'
,
{
name
:
'Submit'
}
)
.
click
(
{
force
:
true
}
)
;
force: trueis an operation that a real user cannot perform, and should not be used in the vast majority of cases. If your test needsforce: true, first check whether there is an interaction design problem.
Navigation actions
page.goto() page navigation
Example
// Basic navigation
await page.
goto
(
'https://www.runoob.com/'
)
;
// Wait for DOMContentLoaded (page structure loaded)
await page.
goto
(
'https://www.runoob.com/'
,
{
waitUntil
:
'domcontentloaded'
}
)
;
// Wait for network idle (all asynchronous data loaded)
await page.
goto
(
'https://www.runoob.com/'
,
{
waitUntil
:
'networkidle'
,
// Network idle
timeout
:
60000
,
// Timeout of 60 seconds
referer
:
'https://google.com'
,
// Referer source
}
)
;
page.goBack() / goForward() / reload()
Example
// Go back one page
await page.
goBack
(
)
;
// Go forward one page
await page.
goForward
(
)
;
// Refresh the page
await page.
reload
(
)
;
Click Operation
locator.click() single click
Example
// Basic click
await page.
getByRole
(
'button'
,
{
name
:
'Login'
}
)
.
click
(
)
;
// Click with options
await page.
getByRole
(
'button'
,
{
name
:
'Login'
}
)
.
click
(
{
button
:
'left'
,
// Mouse button: 'left' | 'right' | 'middle'
clickCount
:
1
,
// Number of clicks
delay
:
100
,
// Delay between press and release (milliseconds)
timeout
:
5000
,
// Timeout duration
modifiers
:
[
]
,
// Modifier keys: ['Alt', 'Control', 'Meta', 'Shift']
}
)
;
locator.dblclick() double-click
Example
// Double-click the element
await page.
getByText
(
'Double-click to edit'
)
.
dblclick
(
)
;
Right Click
Example
// Right-click (open the context menu)
await page.
getByText
(
'Right-click menu'
)
.
click
(
{
button
:
'right'
}
)
;
Modifier-key clicks
Example
// Ctrl + click (multi-select)
await page.
getByRole
(
'option'
)
.
click
(
{
modifiers
:
[
'Control'
]
}
)
;
// Shift + click (range selection / open in a new window)
await page.
getByRole
(
'link'
)
.
click
(
{
modifiers
:
[
'Shift'
]
}
)
;
Click at coordinates
Example
// Click a specific position of the element (relative to its top-left corner)
await page.
locator
(
'canvas'
)
.
click
(
{
position
:
{
x
:
100
,
y
:
50
}
,
}
)
;
locator.hover() hover
Example
// Hover the mouse (trigger a hover effect or dropdown menu)
await page.
getByRole
(
'link'
,
{
name
:
'Tutorial'
}
)
.
hover
(
)
;
// Click the pop-up submenu after hovering
await page.
getByRole
(
'link'
,
{
name
:
'Playwright Tutorial'
}
)
.
click
(
)
;
Scenario differences between click and hover
| Operation | Applicable scenarios |
|---|---|
click() |
Button clicks, link navigation, checkbox toggling |
dblclick() |
Entering edit mode, double-clicking to select text |
click({ button: 'right' }) |
Opening the browser's native context menu |
hover() |
Triggering hover menus, Tooltip prompts |
click({ position }) |
Canvas operations, custom graphic interactions |
Text input actions
locator.fill() clear and fill
fill() It first clears the input box and then fills in the new value; it is the most commonly used and recommended method for input actions.
Example
// Clear the input box and fill in a new value
await page.
getByLabel
(
'Username'
)
.
fill
(
'runoob_user'
)
;
await page.
getByLabel
(
'Password'
)
.
fill
(
'secure_password'
)
;
await page.
getByPlaceholder
(
'Search RUNOOB Tutorial'
)
.
fill
(
'Playwright'
)
;
locator.type() type character by character
type() It types character by character, simulating real keystrokes, with a configurable interval between each key.
Example
// Type character by character (100ms delay between characters)
await page.
getByLabel
(
'Search'
)
.
type
(
'Playwright'
,
{
delay
:
100
}
)
;
The difference between fill and type
| Method | Behavior | Applicable scenarios |
|---|---|---|
fill() |
Clear and fill all at once | The vast majority of scenarios (recommended) |
type() |
Type character by character, triggering a key event each time | When you need to trigger input suggestions or real-time search |
In most cases, using
fill()is sufficient. Only when the input box has real-time search (keydown/keyup events) do you need to usetype()。
locator.clear() clear
Example
// Clear the contents of the input box
await page.
getByLabel
(
'Username'
)
.
clear
(
)
;
Selection actions
locator.check() / uncheck() checkbox
Example
// Check the checkbox
await page.
getByLabel
(
'I agree to the service agreement'
)
.
check
(
)
;
// Uncheck
await page.
getByLabel
(
'Receive email notifications'
)
.
uncheck
(
)
;
// Set the checked state
await page.
getByLabel
(
'Remember me'
)
.
setChecked
(
true
)
;
// Assert it is checked
await page.
getByLabel
(
'Remember me'
)
.
setChecked
(
false
)
;
// Assert it is unchecked
locator.selectOption() dropdown selection
Example
// Select by the value attribute
await page.
getByLabel
(
'City'
)
.
selectOption
(
'beijing'
)
;
// Select by visible text
await page.
getByLabel
(
'City'
)
.
selectOption
(
{
label
:
'Shanghai'
}
)
;
// Multi-select dropdown
await page.
getByLabel
(
'Interest tags'
)
.
selectOption
(
[
{
label
:
'Programming'
}
,
{
value
:
'design'
}
,
]
)
;
// Select by index (item 0, item 2)
await page.
getByLabel
(
'City'
)
.
selectOption
(
{
index
:
0
}
)
;
Keyboard operations
page.keyboard.press() key press
Example
// Press and release Enter
await page.
keyboard
.
press
(
'Enter'
)
;
// Select all (Control + A)
await page.
keyboard
.
press
(
'Control+A'
)
;
// Copy and paste
await page.
keyboard
.
press
(
'Control+C'
)
;
await page.
keyboard
.
press
(
'Control+V'
)
;
// Undo
await page.
keyboard
.
press
(
'Control+Z'
)
;
page.keyboard.down() / up() press/release
Example
// Hold Shift without releasing
await page.
keyboard
.
down
(
'Shift'
)
;
// Press other keys during this time (equivalent to holding Shift and pressing another key)
await page.
keyboard
.
press
(
'ArrowRight'
)
;
// Shift + Right Arrow (select text)
await page.
keyboard
.
press
(
'ArrowRight'
)
;
// Release Shift
await page.
keyboard
.
up
(
'Shift'
)
;
Special key names
| keys | Corresponding key |
|---|---|
Enter |
Carriage return |
Escape |
Esc exit |
Tab |
Tab switch focus |
Backspace |
Backspace delete |
Delete |
Delete key |
ArrowUp/Down/Left/Right |
Arrow keys |
PageUp/PageDown |
Page Up / Page Down keys |
Home/End |
Home / End |
Control/Alt/Shift/Meta |
Modifier keys |
page.keyboard.type() type text via keyboard
Example
// Type text on the focused element
await page.
keyboard
.
type
(
'RUNOOB Tutorial'
,
{
delay
:
50
}
)
;
page.keyboard.insertText() insert directly
Example
// Insert text directly at the focus position (does not simulate keyboard events, fastest)
await page.
keyboard
.
insertText
(
'Quickly inserted text'
)
;
Mouse operations
Coordinate clicking and moving
Example
// Click at viewport coordinates (100, 200)
await page.
mouse
.
click
(
100
,
200
)
;
// Double-click at coordinates
await page.
mouse
.
dblclick
(
100
,
200
)
;
// Move the mouse to coordinates
await page.
mouse
.
move
(
100
,
200
)
;
// Press / release
await page.
mouse
.
down
(
)
;
await page.
mouse
.
up
(
)
;
Mouse wheel
Example
// Scroll down 500px
await page.
mouse
.
wheel
(
0
,
500
)
;
// Scroll left 200px
await page.
mouse
.
wheel
(
200
,
0
)
;
locator.dragTo() drag and drop
Example
// Drag element A to element B
const
source
=
page.
getByText
(
'Drag me'
)
;
const
target
=
page.
getByTestId
(
'drop-zone'
)
;
await source.
dragTo
(
target
)
;
// Drag with options
await source.
dragTo
(
target
,
{
sourcePosition
:
{
x
:
0
,
y
:
0
}
,
// Drag start point (relative to the source element)
targetPosition
:
{
x
:
10
,
y
:
10
}
,
// Drag target point (relative to the target element)
}
)
;
Choosing between page.mouse and locator.click
| Method | Applicable scenarios |
|---|---|
locator.click() |
Clicking page elements (the vast majority of scenarios) |
page.mouse.click() |
Clicking specific coordinates (Canvas, charts, and other DOM-less scenarios) |
File upload
locator.setInputFiles() single/multiple files
Example
// Upload a single file
await page
.
getByLabel
(
'Select a file'
)
.
setInputFiles
(
'path/to/document.pdf'
)
;
// Upload multiple files
await page
.
getByLabel
(
'Select a file'
)
.
setInputFiles
(
[
'path/to/file1.png'
,
'path/to/file2.png'
,
]
)
;
// Clear selected files
await page.
getByLabel
(
'Select a file'
)
.
setInputFiles
(
[
]
)
;
File paths are relative to the current working directory (the project root).
File Download
Example
// Start waiting for the download event (must be called before the action that triggers the download)
const
downloadPromise
=
page.
waitForEvent
(
'download'
)
;
// Perform the action that triggers the download
await page.
getByRole
(
'button'
,
{
name
:
'Download report'
}
)
.
click
(
)
;
// Wait for the download to complete
const
download
=
await downloadPromise
;
// Get the suggested filename
console.
log
(
download.
suggestedFilename
(
)
)
;
// Save to the specified path
await download.
saveAs
(
'downloads/report.pdf'
)
;