私は人形を使ってページに移動しようとしています。ウェブアプリケーションが特定の状態になるのを待って、スクリーンショットを撮って終了します。 SPAがスクリーンショットを撮りたい状態にあるとき、SPAは機能を呼び出します。私は非同期のjsコードの周りに頭をラップするのが難しいです。次のように公開された機能が呼び出された後、人形がスクリーンショットを撮る
私のコードでは、なります「。セッションを閉じたほとんどのページが閉じられた」
const puppeteer = require('puppeteer');
const url = 'http://example.com/';
const width = 300;
const height = 250;
const path = '/vagrant/tmp/screenshot.jpg';
async function takeScreenshoot(url, width, height, path) {
"use strict";
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({width: width, height: height});
await page.exposeFunction('interestingFunction', (async data => {
console.log('Interesting function has been called. Taking a screenshot now.');
await page.screenshot({path: path});
await browser.close();
}));
await page.goto(url);
}
(async() => {
"use strict";
await takeScreenshoot(url, width, height, path);
})();
をしかし、私はscreenshot.js
を呼び出すときに、私が言って未処理の約束についての警告を取得します
node --trace-warnings screenshot.js
Interesting function has been called. Taking a screenshot now.
(node:2572) Error: Protocol error (Runtime.evaluate): Session closed. Most likely the page has been closed.
at Session.send (/vagrant/node_modules/puppeteer/lib/Connection.js:167:29)
at Page._onConsoleAPI (/vagrant/node_modules/puppeteer/lib/Page.js:296:20)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:2572) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
at emitWarning (internal/process/promises.js:78:15)
at emitPendingUnhandledRejections (internal/process/promises.js:95:11)
at process._tickCallback (internal/process/next_tick.js:189:7)
私はライン18からawait browser.close()
を削除すると、そこには警告はありませんが、スクリプトが終了したことがありません。
今や、interstingFunction()
はもう少しですが、webappのウィンドウに公開することは安全です。私はちょうど失敗した上記の最小限のスクリプトの例を挙げようとしました。
私はノードv8.5.0を使用しています。
私はこれに間違っていますか?
あなた 'page.goto(URL)の'の呼び出しは、見当違いのようです。 – nilobarp
私はhttps://github.com/GoogleChrome/puppeteer/blob/master/examples/custom-event.jsの例にしたがって機能を公開してから、ページに移動します。また、 'await browser.close();'は、アプリケーションが読み込まれてから数秒後に呼び出されるコールバックで発生するので、実際には問題ではありません。 'await page.goto(url);'を移動すると警告が消えませんでした。 – hjm27