nodejs program to run puppyteerjs fails - node.js

I have written a very simple puppyteerjs program :
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({ headless: false,
slowMo: 250 });
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();
PS D:\js\puppe> node .\screenshot.js
(node:2052) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md
at onClose (D:\js\puppe\node_modules\puppeteer\lib\Launcher.js:750:14)
at ChildProcess.helper.addEventListener (D:\js\puppe\node_modules\puppeteer\lib\Launcher.js:740:61)
at ChildProcess.emit (events.js:194:15)
at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
(node:2052) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2052) [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.
I tried this program with node version 12 and node version 10, I tried with a headless flag and also giving the path of chrome.exe file from node modules but it keeps on throwing the same exception.
My current environment is: windows 10 and node 10

Related

Error of launching chrome-linux with puppeteer in Centos

I use puppeteer-core with chrome-linux from https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/1095492/chrome-linux.zip.
this is my project:
project directory
// this is index.js
const puppeteer = require('puppeteer-core');
(async () => {
const browser = await puppeteer.launch({ headless: true, executablePath: './chrome-linux/chrome', args: ['--no-sandbox'] });
const page = await browser.newPage();
await page.goto('https://www.baidu.com/');
// Set screen size
await page.setViewport({ width: 1080, height: 1024 });
await browser.close();
})();
I excute node index.js, then the error came out.
(node:32513) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
./chrome-linux/chrome: symbol lookup error: ./chrome-linux/chrome: undefined symbol: gbm_bo_get_modifier
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
at onClose (/opt/xxx/example/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:299:20)
at Interface.<anonymous> (/opt/xxx/example/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:287:24)
at Interface.emit (events.js:412:35)
at Interface.close (readline.js:530:8)
at Socket.onend (readline.js:254:10)
at Socket.emit (events.js:412:35)
at endReadableNT (internal/streams/readable.js:1333:12)
at processTicksAndRejections (internal/process/task_queues.js:82:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:32513) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:32513) [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.
I could not launch Chrome. How can I fix this error? Thanks.
Puppeteer version
19.7.0
Node version
14.21.1
Package manager
npm
Package manager version
6.14.17
Operating system
Linux

Node.js Problem with puppeteer headless:True : " Evaluation failed: TypeError: Cannot read property 'querySelectorAll' of null"

My code works perfectly well with headless false, but when I change it to true I get this error :
(node:92024) UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Cannot read property 'querySelectorAll' of null
at __puppeteer_evaluation_script__:7:17
at ExecutionContext._evaluateInternal (C:\wamp64\www\scrappingWithNodejs\testDiscordBot\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221:19)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async ExecutionContext.evaluate (C:\wamp64\www\scrappingWithNodejs\testDiscordBot\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:110:16)
at async getAllInfos (C:\wamp64\www\scrappingWithNodejs\testDiscordBot\index.js:28:12)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:92024) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:92024) [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.
My code :
async function getAllInfos() {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
page.setDefaultNavigationTimeout(0);
await page.goto(`https://www.japanread.cc/`, {
waitUntil: ["load", "domcontentloaded", "networkidle0", "networkidle2"],
}); //se rendre sur une page
let trs = [];
trs = tbody.querySelectorAll("tr");//the error is here
I tried to put showMo, put several things in the "waitUntil" but I get nothing, I really don't understand.
Do you know what the problem is?

Headless Chrome browser fail to launch on windows container?

I'm using windows nanoserver container to launch puppeteer but it fails and I'm not sure what's wrong with my code
here's my docker file
FROM mcr.microsoft.com/windows/nanoserver:1903
COPY nodejs /windows/system32
CMD [ "node.exe" ]
COPY package.json ./
RUN npm install
# Bundle app source
COPY . .
ENTRYPOINT [ "node", "./server.js" ]
nodejs is a the node folder on my machine
and here's my package.json
{
"dependencies": {
"puppeteer": "^5.5.0",
"#axe-core/puppeteer": "^4.1.0"
}
}
and here's my server.js
const { AxePuppeteer } = require('#axe-core/puppeteer');
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({ headless: true,
ignoreDefaultArgs: ['--disable-extensions'],
args: ['--disable-dev-shm-usage', '--no-sandbox', '--disable-gpu', '--disable-setuid-sandbox'],
});
const page = await browser.newPage();
await page.goto('https://stackoverflow.com/');
const results = await new AxePuppeteer(page).analyze();
console.log(results);
await page.close();
await browser.close();
})();
So if I build the docker image then run
docker build -t chromeHeadless
docker run chromeHeadless
I get this error
(node:1228) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
at onClose (C:\app\node_modules\puppeteer\lib\cjs\puppeteer\node\BrowserRunner.js:193:20) at Interface.<anonymous> (C:\app\node_modules\puppeteer\lib\cjs\puppeteer\node\BrowserRunner.js:183:68)
at Interface.emit (events.js:326:22)
at Interface.close (readline.js:424:8)
at Socket.onend (readline.js:202:10)
at Socket.emit (events.js:326:22)
at endReadableNT (_stream_readable.js:1244:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:1228) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1228) [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.

getting error DeprecationWarning: Unhandled promise rejections are deprecated. in node js

i have create test.js file, and installed one npm library, when i run the file i am getting below error, i tried to use npm i puppeteer this library but i am getting error when i tried to install it, so i used this library npm i puppeteer-core
(node:30075) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:30075) [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.
here is my full code for test.js file
const puppeteer = require('puppeteer-core');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();
can anyone please help me why i am getting this error ?
When using async/await, it is recommended to use try/catch catch the error, and to figure out what's wrong.
(async () => {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
} catch (error) {
console.log('error', error);
// Do whatever you want, throw the error again if you want but it will just produce `UnhandledPromiseRejectionWarning` again, if you throw it again.
}
})();

Chrome launcher exits with UnhandledPromiseRejectionWarning

I am trying to set up chrome-launcher to output all console messages to terminal. My code looks like this
const chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface');
(async function() {
async function launchChrome() {
return await chromeLauncher.launch({
chromeFlags: [
'--window-size=1200,800',
'--user-data-dir=/tmp/chrome-testing',
'--auto-open-devtools-for-tabs'
]
});
}
const chrome = await launchChrome();
const protocol = await CDP({
port: chrome.port
});
const {
DOM,
Network,
Page,
Runtime,
Console
} = protocol;
await Promise.all([Network.enable(), Page.enable(), DOM.enable(), Runtime.enable(), Console.enable()]).catch(console.log);
// REMARKS: messageAdded is fired every time a new console message is added
Console.messageAdded((result) => {
console.log(result);
});
})();
I copied some of this from the question here: How to get console.log output in Terminal via Headless Chrome Runtime.evaluate
When I try to navigate to a page, none of the console messages show up in the terminal, and the chrome-launcher exits with the following error:
(node:14531) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:64656
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1083:14)
(node:14531) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14531) [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.

Resources