跳到正文
本页尚未翻译成中文,以下为英文原文。 查看英文版

Mouse ​

Added in: v1.8

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

java
// 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 ​

mouse.click() ​

Added in: v1.8

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

Arguments

  • 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 Object (optional)

    • button "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.

mouse.dblclick() ​

Added in: v1.8

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

Arguments

  • 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 Object (optional)

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

      Defaults to left.

    • delay float (optional)

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

mouse.down() ​

Added in: v1.8

Dispatches a mousedown event.

Arguments

  • options Object (optional)

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

      Defaults to left.

    • clickCount int (optional)

      defaults to 1. See [UIEvent.detail].

mouse.move() ​

Added in: v1.8

Dispatches a mousemove event.

Arguments

  • 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 Object (optional)

    • steps int (optional)

      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.

mouse.up() ​

Added in: v1.8

Dispatches a mouseup event.

Arguments

  • options Object (optional)

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

      Defaults to left.

    • clickCount int (optional)

      defaults to 1. See [UIEvent.detail].

mouse.wheel() ​

Added in: v1.15

Dispatches a wheel event. This method is usually used to manually scroll the page. See scrolling for alternative ways to scroll.

INFO

Wheel events may cause scrolling if they are not handled, and this method does not wait for the scrolling to finish before returning.

Arguments

  • deltaX float

    Pixels to scroll horizontally.

  • deltaY float

    Pixels to scroll vertically.

基于 Apache 2.0 许可证发布。