I created a function called waitForTimeout
in order to add a simple delay that can be awaited once my browser opens a new page.
function waitForTimeout(timeout) {
return new Promise((resolve) =\> {
setTimeout(resolve, timeout)
})
}
const puppet = () =\> {
getCookies(async (cookies) =\> {
const browser = await puppeteer.launch({
headless: false
});
const page = await browser.newPage();
await page.setCookie(...cookies);
await page.goto("https://ipchicken.com");
await waitForTimeout(10000)
let data = await page.evaluate(()=\> {
const ip = document.querySelector("body \> table:nth-child(2) \> tbody \> tr \> td:nth-child(3) \> p:nth-child(2) \> font \> b").textContent
return ip;
})
console.log(data)
await browser.close();
});
}
I expected the browser to create a page and then wait for however long I want. However, the browser closes immediately but in node there's a delay. Shouldn't the await browser.closer()
only run AFTER await waitForTimeout(10000)
finishes running (i.e. after the promise resolves)?
Here's the error that I'm seeing:
/Users/macbook/repos/operation-tss/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:316
return Promise.reject(new Error(`Protocol error (${method}): Session closed. Most likely the ${__classPrivateFieldGet(this, _CDPSessionImpl_targetType, "f")} has been closed.`));
^
Error: Protocol error (Runtime.callFunctionOn): Session closed. Most likely the page has been closed.
at CDPSessionImpl.send (/Users/macbook/repos/operation-tss/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:316:35)
at ExecutionContext._ExecutionContext_evaluate (/Users/macbook/repos/operation-tss/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:211:46)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async ExecutionContext.evaluate (/Users/macbook/repos/operation-tss/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:107:16)