Use getBoundingClientRect instead of DOM.BoxModel (#76)
This patch re-implements `page.click()` and `page.focus()` using `page.evaluate`
This commit is contained in:
parent
56619baa64
commit
4bd855c66b
70
lib/Page.js
70
lib/Page.js
@ -509,31 +509,36 @@ class Page extends EventEmitter {
|
|||||||
* @param {!Promise}
|
* @param {!Promise}
|
||||||
*/
|
*/
|
||||||
async click(selector) {
|
async click(selector) {
|
||||||
try {
|
let center = await this.evaluate(selector => {
|
||||||
const nodeId = await this._querySelector(selector);
|
let node = document.querySelector(selector);
|
||||||
let boxModel = (await this._client.send('DOM.getBoxModel', { nodeId })).model.content;
|
if (!node)
|
||||||
let x = Math.round((boxModel[0] + boxModel[4]) / (2 * this._screenDPI));
|
return null;
|
||||||
let y = Math.round((boxModel[1] + boxModel[5]) / (2 * this._screenDPI));
|
let rect = node.getBoundingClientRect();
|
||||||
|
return {
|
||||||
this._client.send('Input.dispatchMouseEvent', {
|
x: (rect.left + rect.right) / 2,
|
||||||
type: 'mouseMoved',
|
y: (rect.top + rect.bottom) / 2
|
||||||
x, y
|
};
|
||||||
});
|
}, selector);
|
||||||
this._client.send('Input.dispatchMouseEvent', {
|
if (!center)
|
||||||
type: 'mousePressed',
|
throw new Error('No node found for selector: ' + selector);
|
||||||
button: 'left',
|
let x = Math.round(center.x / this._screenDPI);
|
||||||
x, y,
|
let y = Math.round(center.y / this._screenDPI);
|
||||||
clickCount: 1
|
this._client.send('Input.dispatchMouseEvent', {
|
||||||
});
|
type: 'mouseMoved',
|
||||||
await this._client.send('Input.dispatchMouseEvent', {
|
x, y
|
||||||
type: 'mouseReleased',
|
});
|
||||||
button: 'left',
|
this._client.send('Input.dispatchMouseEvent', {
|
||||||
x, y,
|
type: 'mousePressed',
|
||||||
clickCount: 1
|
button: 'left',
|
||||||
});
|
x, y,
|
||||||
} finally {
|
clickCount: 1
|
||||||
await this._client.send('DOM.disable');
|
});
|
||||||
}
|
await this._client.send('Input.dispatchMouseEvent', {
|
||||||
|
type: 'mouseReleased',
|
||||||
|
button: 'left',
|
||||||
|
x, y,
|
||||||
|
clickCount: 1
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -541,12 +546,15 @@ class Page extends EventEmitter {
|
|||||||
* @param {!Promise}
|
* @param {!Promise}
|
||||||
*/
|
*/
|
||||||
async focus(selector) {
|
async focus(selector) {
|
||||||
try {
|
let success = await this.evaluate(selector => {
|
||||||
const nodeId = await this._querySelector(selector);
|
let node = document.querySelector(selector);
|
||||||
await this._client.send('DOM.focus', { nodeId });
|
if (!node)
|
||||||
} finally {
|
return false;
|
||||||
await this._client.send('DOM.disable');
|
node.focus();
|
||||||
}
|
return true;
|
||||||
|
}, selector);
|
||||||
|
if (!success)
|
||||||
|
throw new Error('No node found for selector: ' + selector);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user