Skip to main content

Mouse

Mouse 类在相对于视口左上角的主框架 CSS 像素中运行。

🌐 The Mouse class operates in main-frame CSS pixels relative to the top-left corner of the viewport.

tip

如果你想调试鼠标的移动位置,可以使用 Trace viewerPlaywright Inspector。每次鼠标操作都会显示一个红点,标示鼠标的位置。

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

每个 page 对象都有自己的鼠标,可以通过 Page.Mouse 访问。

🌐 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

Mouse.MoveAsync()Mouse.DownAsync()Mouse.UpAsync() 的快捷方式。

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

用法

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

参数

  • x [float]#

    相对于主框架视口的 X 坐标(以 CSS 像素为单位)。

  • y [float]#

    相对于主框架视口的 Y 坐标(以 CSS 像素为单位)。

  • options MouseClickOptions? (optional)

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

      默认为 left

    • ClickCount int? (optional)#

      默认为 1。请参阅 UIEvent.detail

    • Delay [float]? (optional)#

      mousedownmouseup 之间的等待时间,以毫秒为单位。默认值为 0。

返回


DblClickAsync

Added before v1.9 mouse.DblClickAsync

Mouse.MoveAsync()Mouse.DownAsync()Mouse.UpAsync()Mouse.DownAsync()Mouse.UpAsync() 的快捷方式。

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

用法

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

参数

  • x [float]#

    相对于主框架视口的 X 坐标(以 CSS 像素为单位)。

  • y [float]#

    相对于主框架视口的 Y 坐标(以 CSS 像素为单位)。

  • options MouseDblClickOptions? (optional)

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

      默认为 left

    • Delay [float]? (optional)#

      mousedownmouseup 之间的等待时间,以毫秒为单位。默认值为 0。

返回


DownAsync

Added before v1.9 mouse.DownAsync

派发一个 mousedown 事件。

🌐 Dispatches a mousedown event.

用法

await Mouse.DownAsync(options);

参数

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

      默认为 left

    • ClickCount int? (optional)#

      默认为 1。请参阅 UIEvent.detail

返回


MoveAsync

Added before v1.9 mouse.MoveAsync

派发一个 mousemove 事件。

🌐 Dispatches a mousemove event.

用法

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

参数

  • x [float]#

    相对于主框架视口的 X 坐标(以 CSS 像素为单位)。

  • y [float]#

    相对于主框架视口的 Y 坐标(以 CSS 像素为单位)。

  • options MouseMoveOptions? (optional)

    • Steps int? (optional)#

      默认为1。发送 n 个插值的 mousemove 事件,以表示从 Playwright 当前光标位置到指定目的地的移动。当设置为1时,在目标位置只发出一个 mousemove 事件。

返回


UpAsync

Added before v1.9 mouse.UpAsync

派发一个 mouseup 事件。

🌐 Dispatches a mouseup event.

用法

await Mouse.UpAsync(options);

参数

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

      默认为 left

    • ClickCount int? (optional)#

      默认为 1。请参阅 UIEvent.detail

返回


WheelAsync

Added in: v1.15 mouse.WheelAsync

分发一个 wheel 事件。此方法通常用于手动滚动页面。有关滚动的其他方法,请参阅 scrolling

🌐 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]#

    水平滚动的像素。

  • deltaY [float]#

    垂直滚动的像素。

返回