Mouse
Mouse 类在相对于视口左上角的主框架 CSS 像素中运行。
🌐 The Mouse class operates in main-frame CSS pixels relative to the top-left corner of the viewport.
如果你想调试鼠标的移动位置,可以使用 Trace viewer 或 Playwright 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().
// 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.9Mouse.move()、Mouse.down()、Mouse.up() 的快捷方式。
🌐 Shortcut for Mouse.move(), Mouse.down(), Mouse.up().
用法
Mouse.click(x, y);
Mouse.click(x, y, options);
参数
-
相对于主框架视口的 X 坐标(以 CSS 像素为单位)。
-
相对于主框架视口的 Y 坐标(以 CSS 像素为单位)。
-
optionsMouse.ClickOptions(optional)
返回
dblclick
Added before v1.9Mouse.move()、Mouse.down()、Mouse.up()、Mouse.down() 和 Mouse.up() 的快捷方式。
🌐 Shortcut for Mouse.move(), Mouse.down(), Mouse.up(), Mouse.down() and Mouse.up().
用法
Mouse.dblclick(x, y);
Mouse.dblclick(x, y, options);
参数
-
相对于主框架视口的 X 坐标(以 CSS 像素为单位)。
-
相对于主框架视口的 Y 坐标(以 CSS 像素为单位)。
-
optionsMouse.DblclickOptions(optional)
返回
down
Added before v1.9派发一个 mousedown 事件。
🌐 Dispatches a mousedown event.
用法
Mouse.down();
Mouse.down(options);
参数
optionsMouse.DownOptions(optional)-
setButtonenum MouseButton { LEFT, RIGHT, MIDDLE }(optional)#默认为
left。 -
默认为 1。请参阅 UIEvent.detail。
-
返回
move
Added before v1.9派发一个 mousemove 事件。
🌐 Dispatches a mousemove event.
用法
Mouse.move(x, y);
Mouse.move(x, y, options);
参数
-
相对于主框架视口的 X 坐标(以 CSS 像素为单位)。
-
相对于主框架视口的 Y 坐标(以 CSS 像素为单位)。
-
optionsMouse.MoveOptions(optional)
返回
up
Added before v1.9派发一个 mouseup 事件。
🌐 Dispatches a mouseup event.
用法
Mouse.up();
Mouse.up(options);
参数
optionsMouse.UpOptions(optional)-
setButtonenum MouseButton { LEFT, RIGHT, MIDDLE }(optional)#默认为
left。 -
默认为 1。请参阅 UIEvent.detail。
-
返回
wheel
Added in: v1.15分发一个 wheel 事件。此方法通常用于手动滚动页面。有关滚动的其他方法,请参阅 scrolling。
🌐 Dispatches a wheel event. This method is usually used to manually scroll the page. See scrolling for alternative ways to scroll.
如果轮子事件未被处理,可能会导致滚动,并且此方法在返回之前不会等待滚动完成。
🌐 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);
参数
返回