mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
parent
2081c12317
commit
64968862db
20
docs/api.md
20
docs/api.md
@ -66,9 +66,9 @@
|
|||||||
+ [keyboard.sendCharacter(char)](#keyboardsendcharacterchar)
|
+ [keyboard.sendCharacter(char)](#keyboardsendcharacterchar)
|
||||||
+ [keyboard.up(key)](#keyboardupkey)
|
+ [keyboard.up(key)](#keyboardupkey)
|
||||||
* [class: Mouse](#class-mouse)
|
* [class: Mouse](#class-mouse)
|
||||||
|
+ [mouse.click(x, y, [options])](#mouseclickx-y-options)
|
||||||
+ [mouse.down([options])](#mousedownoptions)
|
+ [mouse.down([options])](#mousedownoptions)
|
||||||
+ [mouse.move(x, y)](#mousemovex-y)
|
+ [mouse.move(x, y)](#mousemovex-y)
|
||||||
+ [mouse.press([options])](#mousepressoptions)
|
|
||||||
+ [mouse.up([options])](#mouseupoptions)
|
+ [mouse.up([options])](#mouseupoptions)
|
||||||
* [class: Dialog](#class-dialog)
|
* [class: Dialog](#class-dialog)
|
||||||
+ [dialog.accept([promptText])](#dialogacceptprompttext)
|
+ [dialog.accept([promptText])](#dialogacceptprompttext)
|
||||||
@ -679,6 +679,16 @@ Dispatches a `keyup` event.
|
|||||||
|
|
||||||
### class: Mouse
|
### class: Mouse
|
||||||
|
|
||||||
|
#### mouse.click(x, y, [options])
|
||||||
|
- `x` <[number]>
|
||||||
|
- `y` <[number]>
|
||||||
|
- `options` <[Object]>
|
||||||
|
- `button` <[string]> `left`, `right`, or `middle`, defaults to `left`.
|
||||||
|
- `clickCount` <[number]> defaults to 1
|
||||||
|
- returns: <[Promise]>
|
||||||
|
|
||||||
|
Shortcut for [`mouse.move`](#mousemovexy), [`mouse.down`](#mousedownkey) and [`mouse.up`](#mouseupkey).
|
||||||
|
|
||||||
#### mouse.down([options])
|
#### mouse.down([options])
|
||||||
- `options` <[Object]>
|
- `options` <[Object]>
|
||||||
- `button` <[string]> `left`, `right`, or `middle`, defaults to `left`.
|
- `button` <[string]> `left`, `right`, or `middle`, defaults to `left`.
|
||||||
@ -694,14 +704,6 @@ Dispatches a `mousedown` event.
|
|||||||
|
|
||||||
Dispatches a `mousemove` event.
|
Dispatches a `mousemove` event.
|
||||||
|
|
||||||
#### mouse.press([options])
|
|
||||||
- `options` <[Object]>
|
|
||||||
- `button` <[string]> `left`, `right`, or `middle`, defaults to `left`.
|
|
||||||
- `clickCount` <[number]> defaults to 1
|
|
||||||
- returns: <[Promise]>
|
|
||||||
|
|
||||||
Shortcut for [`mouse.down`](#mousedownkey) and [`mouse.up`](#mouseupkey).
|
|
||||||
|
|
||||||
#### mouse.up([options])
|
#### mouse.up([options])
|
||||||
- `options` <[Object]>
|
- `options` <[Object]>
|
||||||
- `button` <[string]> `left`, `right`, or `middle`, defaults to `left`.
|
- `button` <[string]> `left`, `right`, or `middle`, defaults to `left`.
|
||||||
|
@ -345,9 +345,9 @@ class Frame {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} selector
|
* @param {string} selector
|
||||||
* @return {!Promise}
|
* @return {!Promise<{x: number, y: number}>}
|
||||||
*/
|
*/
|
||||||
async hover(selector) {
|
async _centerOfElement(selector) {
|
||||||
let center = await this.evaluate(selector => {
|
let center = await this.evaluate(selector => {
|
||||||
let element = document.querySelector(selector);
|
let element = document.querySelector(selector);
|
||||||
if (!element)
|
if (!element)
|
||||||
@ -361,7 +361,16 @@ class Frame {
|
|||||||
}, selector);
|
}, selector);
|
||||||
if (!center)
|
if (!center)
|
||||||
throw new Error('No node found for selector: ' + selector);
|
throw new Error('No node found for selector: ' + selector);
|
||||||
await this._mouse.move(center.x, center.y);
|
return center;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} selector
|
||||||
|
* @return {!Promise}
|
||||||
|
*/
|
||||||
|
async hover(selector) {
|
||||||
|
let {x, y} = await this._centerOfElement(selector);
|
||||||
|
await this._mouse.move(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -370,10 +379,8 @@ class Frame {
|
|||||||
* @return {!Promise}
|
* @return {!Promise}
|
||||||
*/
|
*/
|
||||||
async click(selector, options) {
|
async click(selector, options) {
|
||||||
await this.hover(selector);
|
let {x, y} = await this._centerOfElement(selector);
|
||||||
await this._mouse.press(options);
|
await this._mouse.click(x, y, options);
|
||||||
// This is a hack for now, to make clicking less race-prone
|
|
||||||
await this.evaluate(() => new Promise(f => requestAnimationFrame(f)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
11
lib/Input.js
11
lib/Input.js
@ -118,11 +118,20 @@ class Mouse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param {number} x
|
||||||
|
* @param {number} y
|
||||||
* @param {!Object=} options
|
* @param {!Object=} options
|
||||||
*/
|
*/
|
||||||
async press(options) {
|
async click(x, y, options) {
|
||||||
|
await this.move(x, y);
|
||||||
await this.down(options);
|
await this.down(options);
|
||||||
await this.up(options);
|
await this.up(options);
|
||||||
|
// This is a hack for now, to make clicking less race-prone. See crbug.com/747647
|
||||||
|
await this._client.send('Runtime.evaluate', {
|
||||||
|
expression: 'new Promise(f => requestAnimationFrame(f))',
|
||||||
|
awaitPromise: true,
|
||||||
|
returnByValue: true
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -466,25 +466,27 @@ class WebPage {
|
|||||||
_sendMouseEvent(eventType, x, y, button, modifier) {
|
_sendMouseEvent(eventType, x, y, button, modifier) {
|
||||||
if (modifier)
|
if (modifier)
|
||||||
await(this._page.keyboard.down(modifier));
|
await(this._page.keyboard.down(modifier));
|
||||||
await(this._page.mouse.move(x, y));
|
|
||||||
switch (eventType) {
|
switch (eventType) {
|
||||||
case 'mousemove':
|
case 'mousemove':
|
||||||
|
await(this._page.mouse.move(x, y));
|
||||||
break;
|
break;
|
||||||
case 'mousedown':
|
case 'mousedown':
|
||||||
|
await(this._page.mouse.move(x, y));
|
||||||
await(this._page.mouse.down({button}));
|
await(this._page.mouse.down({button}));
|
||||||
break;
|
break;
|
||||||
case 'mouseup':
|
case 'mouseup':
|
||||||
|
await(this._page.mouse.move(x, y));
|
||||||
await(this._page.mouse.up({button}));
|
await(this._page.mouse.up({button}));
|
||||||
break;
|
break;
|
||||||
case 'doubleclick':
|
case 'doubleclick':
|
||||||
await(this._page.mouse.press({button}));
|
await(this._page.mouse.click(x, y, {button}));
|
||||||
await(this._page.mouse.press({button, clickCount: 2}));
|
await(this._page.mouse.click(x, y, {button, clickCount: 2}));
|
||||||
break;
|
break;
|
||||||
case 'click':
|
case 'click':
|
||||||
await(this._page.mouse.press({button}));
|
await(this._page.mouse.click(x, y, {button}));
|
||||||
break;
|
break;
|
||||||
case 'contextmenu':
|
case 'contextmenu':
|
||||||
await(this._page.mouse.press({button: 'right'}));
|
await(this._page.mouse.click(x, y, {button: 'right'}));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (modifier)
|
if (modifier)
|
||||||
|
Loading…
Reference in New Issue
Block a user