The Mouse class operates in main-frame CSS pixels relative to the top-left corner of the viewport.
Tip
If you want to debug where the mouse moved, you can use the Trace viewer or Playwright Inspector. A red dot showing the location of the mouse will be shown for every mouse action.
Every page object has its own Mouse, accessible with Playwright.Page.mouse/1.
# Using Page.mouse/1 to trace a 100x100 square.
mouse = Page.mouse(page)
Mouse.move!(mouse, 0, 0)
Mouse.down!(mouse)
Mouse.move!(mouse, 0, 100)
Mouse.move!(mouse, 100, 100)
Mouse.move!(mouse, 100, 0)
Mouse.move!(mouse, 0, 0)
Mouse.up!(mouse)Since Playwright 1.8.
The examples alias the modules they use (alias Playwright.{Locator, Page}).
Summary
Types
Options of click/4, see its docs.
Options of dblclick/4, see its docs.
Options of down/2, see its docs.
Options of move/4, see its docs.
A Mouse handle. Compare handles with ==, their fields are internal.
Options of up/2, see its docs.
Functions
Shortcut for Playwright.Mouse.move/4, Playwright.Mouse.down/2, Playwright.Mouse.up/2.
Like click/4, but returns the value and raises on error.
Like dblclick/4, but returns the value and raises on error.
Dispatches a mousedown event.
Like down/2, but returns the value and raises on error.
Dispatches a mousemove event.
Like move/4, but returns the value and raises on error.
Dispatches a mouseup event.
Like up/2, but returns the value and raises on error.
Dispatches a wheel event. This method is usually used to manually scroll the page. See scrolling for alternative ways to scroll.
Like wheel/3, but returns the value and raises on error.
Types
@type click_options() :: [button: :left | :right | :middle, click_count: integer(), delay: number()] | %{ optional(:button) => :left | :right | :middle, optional(:click_count) => integer(), optional(:delay) => number() }
Options of click/4, see its docs.
@type dblclick_options() :: [button: :left | :right | :middle, delay: number()] | %{ optional(:button) => :left | :right | :middle, optional(:delay) => number() }
Options of dblclick/4, see its docs.
@type down_options() :: [button: :left | :right | :middle, click_count: integer()] | %{ optional(:button) => :left | :right | :middle, optional(:click_count) => integer() }
Options of down/2, see its docs.
Options of move/4, see its docs.
@type t() :: %Playwright.Mouse{_page: term()}
A Mouse handle. Compare handles with ==, their fields are internal.
@type up_options() :: [button: :left | :right | :middle, click_count: integer()] | %{ optional(:button) => :left | :right | :middle, optional(:click_count) => integer() }
Options of up/2, see its docs.
Functions
@spec click(t(), number(), number(), click_options()) :: :ok | {:error, Playwright.error()}
Shortcut for Playwright.Mouse.move/4, Playwright.Mouse.down/2, Playwright.Mouse.up/2.
Since Playwright 1.8.
Arguments
x- X coordinate relative to the main frame's viewport in CSS pixels.y- Y coordinate relative to the main frame's viewport in CSS pixels.
Options
:button(:left|:right|:middle) - Defaults to:left.:click_count(integer/0) - defaults to 1. SeeUIEvent.detail.:delay(number/0) - Time to wait betweenmousedownandmouseupin milliseconds. Defaults to 0.
@spec click!(t(), number(), number(), click_options()) :: :ok
Like click/4, but returns the value and raises on error.
@spec dblclick(t(), number(), number(), dblclick_options()) :: :ok | {:error, Playwright.error()}
Shortcut for Playwright.Mouse.move/4, Playwright.Mouse.down/2, Playwright.Mouse.up/2, Playwright.Mouse.down/2 and
Playwright.Mouse.up/2.
Since Playwright 1.8.
Arguments
x- X coordinate relative to the main frame's viewport in CSS pixels.y- Y coordinate relative to the main frame's viewport in CSS pixels.
Options
:button(:left|:right|:middle) - Defaults to:left.:delay(number/0) - Time to wait betweenmousedownandmouseupin milliseconds. Defaults to 0.
@spec dblclick!(t(), number(), number(), dblclick_options()) :: :ok
Like dblclick/4, but returns the value and raises on error.
@spec down(t(), down_options()) :: :ok | {:error, Playwright.error()}
Dispatches a mousedown event.
Since Playwright 1.8.
Options
:button(:left|:right|:middle) - Defaults to:left.:click_count(integer/0) - defaults to 1. SeeUIEvent.detail.
@spec down!(t(), down_options()) :: :ok
Like down/2, but returns the value and raises on error.
@spec move(t(), number(), number(), move_options()) :: :ok | {:error, Playwright.error()}
Dispatches a mousemove event.
Since Playwright 1.8.
Arguments
x- X coordinate relative to the main frame's viewport in CSS pixels.y- Y coordinate relative to the main frame's viewport in CSS pixels.
Options
:steps(integer/0) - Defaults to 1. Sendsninterpolatedmousemoveevents to represent travel between Playwright's current cursor position and the provided destination. When set to 1, emits a singlemousemoveevent at the destination location.
@spec move!(t(), number(), number(), move_options()) :: :ok
Like move/4, but returns the value and raises on error.
@spec up(t(), up_options()) :: :ok | {:error, Playwright.error()}
Dispatches a mouseup event.
Since Playwright 1.8.
Options
:button(:left|:right|:middle) - Defaults to:left.:click_count(integer/0) - defaults to 1. SeeUIEvent.detail.
@spec up!(t(), up_options()) :: :ok
Like up/2, but returns the value and raises on error.
@spec wheel(t(), number(), number()) :: :ok | {:error, Playwright.error()}
Dispatches a wheel event. This method is usually used to manually scroll the page. See scrolling for alternative ways to scroll.
Note
Wheel events may cause scrolling if they are not handled, and this method does not wait for the scrolling to finish before returning.
Since Playwright 1.15.
Arguments
delta_x- Pixels to scroll horizontally.delta_y- Pixels to scroll vertically.
Like wheel/3, but returns the value and raises on error.