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.

每个 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.9 mouse.click

Mouse.move()Mouse.down()Mouse.up() 的快捷方式。

¥Shortcut for Mouse.move(), Mouse.down(), Mouse.up().

用法

¥Usage

Mouse.click(x, y);
Mouse.click(x, y, options);

参数

¥Arguments

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

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

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

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

  • options Mouse.ClickOptions(可选)

    ¥options Mouse.ClickOptions (optional)

    • setButton enum MouseButton { LEFT, RIGHT, MIDDLE }(可选)#

      ¥setButton enum MouseButton { LEFT, RIGHT, MIDDLE } (optional)#

      默认为 left

      ¥Defaults to left.

    • setClickCount int (optional)#

默认为 1。参见 UIEvent.detail

¥defaults to 1. See UIEvent.detail.

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

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

返回

¥Returns


dblclick

Added before v1.9 mouse.dblclick

Mouse.move()Mouse.down()Mouse.up()Mouse.down()Mouse.up() 的快捷方式。

¥Shortcut for Mouse.move(), Mouse.down(), Mouse.up(), Mouse.down() and Mouse.up().

用法

¥Usage

Mouse.dblclick(x, y);
Mouse.dblclick(x, y, options);

参数

¥Arguments

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

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

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

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

  • options Mouse.DblclickOptions(可选)

    ¥options Mouse.DblclickOptions (optional)

    • setButton enum MouseButton { LEFT, RIGHT, MIDDLE }(可选)#

      ¥setButton enum MouseButton { LEFT, RIGHT, MIDDLE } (optional)#

      默认为 left

      ¥Defaults to left.

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

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

返回

¥Returns


down

Added before v1.9 mouse.down

调度 mousedown 事件。

¥Dispatches a mousedown event.

用法

¥Usage

Mouse.down();
Mouse.down(options);

参数

¥Arguments

  • options Mouse.DownOptions(可选)

    ¥options Mouse.DownOptions (optional)

    • setButton enum MouseButton { LEFT, RIGHT, MIDDLE }(可选)#

      ¥setButton enum MouseButton { LEFT, RIGHT, MIDDLE } (optional)#

      默认为 left

      ¥Defaults to left.

    • setClickCount int (optional)#

默认为 1。参见 UIEvent.detail

¥defaults to 1. See UIEvent.detail.

返回

¥Returns


move

Added before v1.9 mouse.move

调度 mousemove 事件。

¥Dispatches a mousemove event.

用法

¥Usage

Mouse.move(x, y);
Mouse.move(x, y, options);

参数

¥Arguments

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

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

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

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

  • options Mouse.MoveOptions(可选)

    ¥options Mouse.MoveOptions (optional)

    • setSteps int (optional)#

默认为 1。发送中间 mousemove 事件。

¥Defaults to 1. Sends intermediate mousemove events.

返回

¥Returns


up

Added before v1.9 mouse.up

调度 mouseup 事件。

¥Dispatches a mouseup event.

用法

¥Usage

Mouse.up();
Mouse.up(options);

参数

¥Arguments

  • options Mouse.UpOptions(可选)

    ¥options Mouse.UpOptions (optional)

    • setButton enum MouseButton { LEFT, RIGHT, MIDDLE }(可选)#

      ¥setButton enum MouseButton { LEFT, RIGHT, MIDDLE } (optional)#

      默认为 left

      ¥Defaults to left.

    • setClickCount int (optional)#

默认为 1。参见 UIEvent.detail

¥defaults to 1. See UIEvent.detail.

返回

¥Returns


wheel

Added in: v1.15 mouse.wheel

调度 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.

用法

¥Usage

Mouse.wheel(deltaX, deltaY);

参数

¥Arguments

水平滚动的像素。

¥Pixels to scroll horizontally.

垂直滚动的像素。

¥Pixels to scroll vertically.

返回

¥Returns