Mouse
The Mouse class operates in main-frame CSS pixels relative to the top-left corner of the viewport.
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.
Every page
object has its own Mouse, accessible with page.mouse.
- Sync
- Async
# 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()
# 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 before v1.9Shortcut for mouse.move(), mouse.down(), mouse.up().
使用方式
mouse.click(x, y)
mouse.click(x, y, **kwargs)
參數
-
X coordinate relative to the main frame's viewport in CSS pixels.
-
Y coordinate relative to the main frame's viewport in CSS pixels.
-
button
"left" | "right" | "middle" (optional)#Defaults to
left
. -
defaults to 1. See UIEvent.detail.
-
Time to wait between
mousedown
andmouseup
in milliseconds. Defaults to 0.
傳回值
dblclick
Added before v1.9Shortcut for mouse.move(), mouse.down(), mouse.up(), mouse.down() and mouse.up().
使用方式
mouse.dblclick(x, y)
mouse.dblclick(x, y, **kwargs)
參數
-
X coordinate relative to the main frame's viewport in CSS pixels.
-
Y coordinate relative to the main frame's viewport in CSS pixels.
-
button
"left" | "right" | "middle" (optional)#Defaults to
left
. -
Time to wait between
mousedown
andmouseup
in milliseconds. Defaults to 0.
傳回值
down
Added before v1.9Dispatches a mousedown
event.
使用方式
mouse.down()
mouse.down(**kwargs)
參數
-
button
"left" | "right" | "middle" (optional)#Defaults to
left
. -
defaults to 1. See UIEvent.detail.
傳回值
move
Added before v1.9Dispatches a mousemove
event.
使用方式
mouse.move(x, y)
mouse.move(x, y, **kwargs)
參數
-
X coordinate relative to the main frame's viewport in CSS pixels.
-
Y coordinate relative to the main frame's viewport in CSS pixels.
-
Defaults to 1. Sends intermediate
mousemove
events.
傳回值
up
Added before v1.9Dispatches a mouseup
event.
使用方式
mouse.up()
mouse.up(**kwargs)
參數
-
button
"left" | "right" | "middle" (optional)#Defaults to
left
. -
defaults to 1. See UIEvent.detail.
傳回值
wheel
Added in: v1.15Dispatches 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(delta_x, delta_y)
參數
傳回值