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.

await Page.Mouse.MoveAsync(0, 0);
await Page.Mouse.DownAsync();
await Page.Mouse.MoveAsync(0, 100);
await Page.Mouse.MoveAsync(100, 100);
await Page.Mouse.MoveAsync(100, 0);
await Page.Mouse.MoveAsync(0, 0);
await Page.Mouse.UpAsync();

方法 (Methods)

ClickAsync

Added before v1.9 mouse.ClickAsync

Shortcut for Mouse.MoveAsync(), Mouse.DownAsync(), Mouse.UpAsync().

使用方式

await Mouse.ClickAsync(x, y, options);

參數

  • x [float]#

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

  • y [float]#

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

  • options MouseClickOptions? (optional)

    • Button enum MouseButton { Left, Right, Middle }? (optional)#

      Defaults to left.

    • ClickCount int? (optional)#

      defaults to 1. See UIEvent.detail.

    • Delay [float]? (optional)#

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

傳回值


DblClickAsync

Added before v1.9 mouse.DblClickAsync

Shortcut for Mouse.MoveAsync(), Mouse.DownAsync(), Mouse.UpAsync(), Mouse.DownAsync() and Mouse.UpAsync().

使用方式

await Mouse.DblClickAsync(x, y, options);

參數

  • x [float]#

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

  • y [float]#

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

  • options MouseDblClickOptions? (optional)

    • Button enum MouseButton { Left, Right, Middle }? (optional)#

      Defaults to left.

    • Delay [float]? (optional)#

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

傳回值


DownAsync

Added before v1.9 mouse.DownAsync

Dispatches a mousedown event.

使用方式

await Mouse.DownAsync(options);

參數

  • options MouseDownOptions? (optional)
    • Button enum MouseButton { Left, Right, Middle }? (optional)#

      Defaults to left.

    • ClickCount int? (optional)#

      defaults to 1. See UIEvent.detail.

傳回值


MoveAsync

Added before v1.9 mouse.MoveAsync

Dispatches a mousemove event.

使用方式

await Mouse.MoveAsync(x, y, options);

參數

  • x [float]#

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

  • y [float]#

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

  • options MouseMoveOptions? (optional)

    • Steps int? (optional)#

      Defaults to 1. Sends intermediate mousemove events.

傳回值


UpAsync

Added before v1.9 mouse.UpAsync

Dispatches a mouseup event.

使用方式

await Mouse.UpAsync(options);

參數

  • options MouseUpOptions? (optional)
    • Button enum MouseButton { Left, Right, Middle }? (optional)#

      Defaults to left.

    • ClickCount int? (optional)#

      defaults to 1. See UIEvent.detail.

傳回值


WheelAsync

Added in: v1.15 mouse.WheelAsync

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.

使用方式

await Mouse.WheelAsync(deltaX, deltaY);

參數

  • deltaX [float]#

    Pixels to scroll horizontally.

  • deltaY [float]#

    Pixels to scroll vertically.

傳回值