Skip to main content

Mouse

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 Page.mouse().

// Using ‘page.mouse’ to trace a 100x100 square.
page.mouse().move(0, 0);
page.mouse().down();
page.mouse().move(0, 100);
page.mouse().move(100, 100);
page.mouse().move(100, 0);
page.mouse().move(0, 0);
page.mouse().up();

方法 (Methods)

click

Added before v1.9 mouse.click

Shortcut for Mouse.move(), Mouse.down(), Mouse.up().

使用方式

Mouse.click(x, y);
Mouse.click(x, y, options);

參數

  • x double#

    X coordinate relative to the main frame's viewport in CSS pixels.

  • y double#

    Y coordinate relative to the main frame's viewport in CSS pixels.

  • options Mouse.ClickOptions (optional)

    • setButton enum MouseButton { LEFT, RIGHT, MIDDLE } (optional)#

      Defaults to left.

    • setClickCount int (optional)#

      defaults to 1. See UIEvent.detail.

    • setDelay double (optional)#

      Time to wait between mousedown and mouseup in milliseconds. Defaults to 0.

傳回值


dblclick

Added before v1.9 mouse.dblclick

Shortcut for Mouse.move(), Mouse.down(), Mouse.up(), Mouse.down() and Mouse.up().

使用方式

Mouse.dblclick(x, y);
Mouse.dblclick(x, y, options);

參數

  • x double#

    X coordinate relative to the main frame's viewport in CSS pixels.

  • y double#

    Y coordinate relative to the main frame's viewport in CSS pixels.

  • options Mouse.DblclickOptions (optional)

    • setButton enum MouseButton { LEFT, RIGHT, MIDDLE } (optional)#

      Defaults to left.

    • setDelay double (optional)#

      Time to wait between mousedown and mouseup in milliseconds. Defaults to 0.

傳回值


down

Added before v1.9 mouse.down

Dispatches a mousedown event.

使用方式

Mouse.down();
Mouse.down(options);

參數

  • options Mouse.DownOptions (optional)
    • setButton enum MouseButton { LEFT, RIGHT, MIDDLE } (optional)#

      Defaults to left.

    • setClickCount int (optional)#

      defaults to 1. See UIEvent.detail.

傳回值


move

Added before v1.9 mouse.move

Dispatches a mousemove event.

使用方式

Mouse.move(x, y);
Mouse.move(x, y, options);

參數

  • x double#

    X coordinate relative to the main frame's viewport in CSS pixels.

  • y double#

    Y coordinate relative to the main frame's viewport in CSS pixels.

  • options Mouse.MoveOptions (optional)

    • setSteps int (optional)#

      Defaults to 1. Sends intermediate mousemove events.

傳回值


up

Added before v1.9 mouse.up

Dispatches a mouseup event.

使用方式

Mouse.up();
Mouse.up(options);

參數

  • options Mouse.UpOptions (optional)
    • setButton enum MouseButton { LEFT, RIGHT, MIDDLE } (optional)#

      Defaults to left.

    • setClickCount int (optional)#

      defaults to 1. See UIEvent.detail.

傳回值


wheel

Added in: v1.15 mouse.wheel

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.

使用方式

Mouse.wheel(deltaX, deltaY);

參數

  • deltaX double#

    Pixels to scroll horizontally.

  • deltaY double#

    Pixels to scroll vertically.

傳回值