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.
await page.mouse.move(0, 0);
await page.mouse.down();
await page.mouse.move(0, 100);
await page.mouse.move(100, 100);
await page.mouse.move(100, 0);
await page.mouse.move(0, 0);
await page.mouse.up();

方法 (Methods)

click

Added before v1.9 mouse.click

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

使用方式

await mouse.click(x, y);
await mouse.click(x, y, options);

參數

  • x number#

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

  • y number#

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

  • options Object (optional)

    • button "left" | "right" | "middle" (optional)#

      Defaults to left.

    • clickCount number (optional)#

      defaults to 1. See UIEvent.detail.

    • delay number (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().

使用方式

await mouse.dblclick(x, y);
await mouse.dblclick(x, y, options);

參數

  • x number#

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

  • y number#

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

  • options Object (optional)

    • button "left" | "right" | "middle" (optional)#

      Defaults to left.

    • delay number (optional)#

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

傳回值


down

Added before v1.9 mouse.down

Dispatches a mousedown event.

使用方式

await mouse.down();
await mouse.down(options);

參數

  • options Object (optional)
    • button "left" | "right" | "middle" (optional)#

      Defaults to left.

    • clickCount number (optional)#

      defaults to 1. See UIEvent.detail.

傳回值


move

Added before v1.9 mouse.move

Dispatches a mousemove event.

使用方式

await mouse.move(x, y);
await mouse.move(x, y, options);

參數

  • x number#

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

  • y number#

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

  • options Object (optional)

    • steps number (optional)#

      Defaults to 1. Sends intermediate mousemove events.

傳回值


up

Added before v1.9 mouse.up

Dispatches a mouseup event.

使用方式

await mouse.up();
await mouse.up(options);

參數

  • options Object (optional)
    • button "left" | "right" | "middle" (optional)#

      Defaults to left.

    • clickCount number (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.

使用方式

await mouse.wheel(deltaX, deltaY);

參數

  • deltaX number#

    Pixels to scroll horizontally.

  • deltaY number#

    Pixels to scroll vertically.

傳回值