Playwright文档 - Mouse(鼠标)


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

class: Mouse

  • since: v1.8

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

Every page object has its own Mouse, accessible with [property: Page.mouse].

1// Using ‘page.mouse’ to trace a 100x100 square. 2await page.mouse.move(0, 0); 3await page.mouse.down(); 4await page.mouse.move(0, 100); 5await page.mouse.move(100, 100); 6await page.mouse.move(100, 0); 7await page.mouse.move(0, 0); 8await page.mouse.up(); 9
1// Using ‘page.mouse’ to trace a 100x100 square. 2page.mouse().move(0, 0); 3page.mouse().down(); 4page.mouse().move(0, 100); 5page.mouse().move(100, 100); 6page.mouse().move(100, 0); 7page.mouse().move(0, 0); 8page.mouse().up(); 9
1# using ‘page.mouse’ to trace a 100x100 square. 2await page.mouse.move(0, 0) 3await page.mouse.down() 4await page.mouse.move(0, 100) 5await page.mouse.move(100, 100) 6await page.mouse.move(100, 0) 7await page.mouse.move(0, 0) 8await page.mouse.up() 9
1# using ‘page.mouse’ to trace a 100x100 square. 2page.mouse.move(0, 0) 3page.mouse.down() 4page.mouse.move(0, 100) 5page.mouse.move(100, 100) 6page.mouse.move(100, 0) 7page.mouse.move(0, 0) 8page.mouse.up() 9
1await Page.Mouse.MoveAsync(0, 0); 2await Page.Mouse.DownAsync(); 3await Page.Mouse.MoveAsync(0, 100); 4await Page.Mouse.MoveAsync(100, 100); 5await Page.Mouse.MoveAsync(100, 0); 6await Page.Mouse.MoveAsync(0, 0); 7await Page.Mouse.UpAsync(); 8

async method: Mouse.click

  • since: v1.8

Shortcut for [method: Mouse.move], [method: Mouse.down], [method: Mouse.up].

param: Mouse.click.x

  • since: v1.8
  • x <[float]>

param: Mouse.click.y

  • since: v1.8
  • y <[float]>

option: Mouse.click.button = %%-input-button-%%

  • since: v1.8

option: Mouse.click.clickCount = %%-input-click-count-%%

  • since: v1.8

option: Mouse.click.delay = %%-input-down-up-delay-%%

  • since: v1.8

async method: Mouse.dblclick

  • since: v1.8
  • langs:
    • alias-csharp: DblClickAsync

Shortcut for [method: Mouse.move], [method: Mouse.down], [method: Mouse.up], [method: Mouse.down] and [method: Mouse.up].

param: Mouse.dblclick.x

  • since: v1.8
  • x <[float]>

param: Mouse.dblclick.y

  • since: v1.8
  • y <[float]>

option: Mouse.dblclick.button = %%-input-button-%%

  • since: v1.8

option: Mouse.dblclick.delay = %%-input-down-up-delay-%%

  • since: v1.8

async method: Mouse.down

  • since: v1.8

Dispatches a mousedown event.

option: Mouse.down.button = %%-input-button-%%

  • since: v1.8

option: Mouse.down.clickCount = %%-input-click-count-%%

  • since: v1.8

async method: Mouse.move

  • since: v1.8

Dispatches a mousemove event.

param: Mouse.move.x

  • since: v1.8
  • x <[float]>

param: Mouse.move.y

  • since: v1.8
  • y <[float]>

option: Mouse.move.steps

  • since: v1.8
  • steps <[int]>

Defaults to 1. Sends intermediate mousemove events.

async method: Mouse.up

  • since: v1.8

Dispatches a mouseup event.

option: Mouse.up.button = %%-input-button-%%

  • since: v1.8

option: Mouse.up.clickCount = %%-input-click-count-%%

  • since: v1.8

async method: Mouse.wheel

  • since: v1.15

Dispatches a wheel event.

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

param: Mouse.wheel.deltaX

  • since: v1.15
  • deltaX <[float]>

Pixels to scroll horizontally.

param: Mouse.wheel.deltaY

  • since: v1.15
  • deltaY <[float]>

Pixels to scroll vertically.