mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
Added frame.waitFor(selector) (#59)
This patch adds `frame.waitFor(selector)` method. Fixes #42.
This commit is contained in:
committed by
Andrey Lushnikov
parent
a0eeb415f2
commit
090ecfa6b9
@@ -197,6 +197,39 @@ class FrameManager extends EventEmitter {
|
||||
});
|
||||
return response.result.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} selector
|
||||
* @param {!Frame} frame
|
||||
* @return {!Promise<undefined>}
|
||||
*/
|
||||
async _waitForInFrame(selector, frame) {
|
||||
let code = selector => new Promise((fulfill, reject) => {
|
||||
if (document.querySelector(selector)) {
|
||||
fulfill();
|
||||
return;
|
||||
}
|
||||
new MutationObserver((mutations, observer) => {
|
||||
for (let mutation of mutations) {
|
||||
for (let node of mutation.addedNodes) {
|
||||
if (node.matches(selector)) {
|
||||
observer.disconnect();
|
||||
fulfill();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}).observe(document.documentElement, {
|
||||
childList: true,
|
||||
subtree: true
|
||||
});
|
||||
});
|
||||
await this._client.send('Runtime.evaluate', {
|
||||
expression: helper.evaluationString(code, selector),
|
||||
awaitPromise: true,
|
||||
returnByValue: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** @enum {string} */
|
||||
@@ -287,6 +320,14 @@ class Frame {
|
||||
return this._detached;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} selector
|
||||
* @return {!Promise<undefined>}
|
||||
*/
|
||||
async waitFor(selector) {
|
||||
await this._frameManager._waitForInFrame(selector, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {?Object} framePayload
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user