Playwright.Mouse (Playwright v1.65.0-rc.3)

Copy Markdown View Source

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.

t()

A Mouse handle. Compare handles with ==, their fields are internal.

Options of up/2, see its docs.

Functions

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

click_options()

@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.

dblclick_options()

@type dblclick_options() ::
  [button: :left | :right | :middle, delay: number()]
  | %{
      optional(:button) => :left | :right | :middle,
      optional(:delay) => number()
    }

Options of dblclick/4, see its docs.

down_options()

@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.

move_options()

@type move_options() :: [{:steps, integer()}] | %{optional(:steps) => integer()}

Options of move/4, see its docs.

t()

@type t() :: %Playwright.Mouse{_page: term()}

A Mouse handle. Compare handles with ==, their fields are internal.

up_options()

@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

click(mouse, x, y, options \\ [])

@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. See UIEvent.detail.
  • :delay (number/0) - Time to wait between mousedown and mouseup in milliseconds. Defaults to 0.

click!(mouse, x, y, options \\ [])

@spec click!(t(), number(), number(), click_options()) :: :ok

Like click/4, but returns the value and raises on error.

dblclick(mouse, x, y, options \\ [])

@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 between mousedown and mouseup in milliseconds. Defaults to 0.

dblclick!(mouse, x, y, options \\ [])

@spec dblclick!(t(), number(), number(), dblclick_options()) :: :ok

Like dblclick/4, but returns the value and raises on error.

down(mouse, options \\ [])

@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. See UIEvent.detail.

down!(mouse, options \\ [])

@spec down!(t(), down_options()) :: :ok

Like down/2, but returns the value and raises on error.

move(mouse, x, y, options \\ [])

@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. Sends n interpolated mousemove events to represent travel between Playwright's current cursor position and the provided destination. When set to 1, emits a single mousemove event at the destination location.

move!(mouse, x, y, options \\ [])

@spec move!(t(), number(), number(), move_options()) :: :ok

Like move/4, but returns the value and raises on error.

up(mouse, options \\ [])

@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. See UIEvent.detail.

up!(mouse, options \\ [])

@spec up!(t(), up_options()) :: :ok

Like up/2, but returns the value and raises on error.

wheel(mouse, delta_x, delta_y)

@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.

wheel!(mouse, delta_x, delta_y)

@spec wheel!(t(), number(), number()) :: :ok

Like wheel/3, but returns the value and raises on error.