Skip to main content

鼠标

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.
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 in: v1.8 mouse.click

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

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

用法

Usage

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

参数

Arguments

  • button "left"|"right"|"middle"(可选)#

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

    默认为 left

    Defaults to left.

默认为 1。参见 UIEvent.detail

defaults to 1. See UIEvent.detail.

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

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


dblclick

Added in: v1.8 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

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

参数

Arguments

  • button "left"|"right"|"middle"(可选)#

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

    默认为 left

    Defaults to left.

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

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


down

Added in: v1.8 mouse.down

调度 mousedown 事件。

Dispatches a mousedown event.

用法

Usage

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

参数

Arguments

  • button "left"|"right"|"middle"(可选)#

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

    默认为 left

    Defaults to left.

默认为 1。参见 UIEvent.detail

defaults to 1. See UIEvent.detail.


move

Added in: v1.8 mouse.move

调度 mousemove 事件。

Dispatches a mousemove event.

用法

Usage

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

参数

Arguments

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

Defaults to 1. Sends intermediate mousemove events.


up

Added in: v1.8 mouse.up

调度 mouseup 事件。

Dispatches a mouseup event.

用法

Usage

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

参数

Arguments

  • button "left"|"right"|"middle"(可选)#

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

    默认为 left

    Defaults to left.

默认为 1。参见 UIEvent.detail

defaults to 1. See UIEvent.detail.


wheel

Added in: v1.15 mouse.wheel

调度 wheel 事件。

Dispatches a wheel event.

注意

滚轮事件如果不处理的话可能会导致滚动,并且该方法不会等待滚动完成才返回。

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

用法

Usage

await mouse.wheel(deltaX, deltaY);

参数

Arguments

水平滚动的像素。

Pixels to scroll horizontally.

垂直滚动的像素。

Pixels to scroll vertically.