Understanding why Puppeteer wont load a website - javascript

I have some simple code
const puppeteer = require('puppeteer');
async function getPic() {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto('https://www.paniniamerica.net');
await page.screenshot({path: 'panini.png'});
await browser.close();
}
getPic();
I noticed that this specific website just times out. Other websites like google.com or even stackoverflow.com work fine. Is this website blocking this type of behavior?
This is the error it spits out.
(node:63995) UnhandledPromiseRejectionWarning: Error: Navigation failed because browser has disconnected!
at CDPSession.LifecycleWatcher._eventListeners.helper.addEventListener (/Users//Desktop/test/node_modules/puppeteer/lib/LifecycleWatcher.js:46:107)
at CDPSession.emit (events.js:182:13)
at CDPSession._onClosed (/Users//Desktop/test/node_modules/puppeteer/lib/Connection.js:215:10)
at Connection._onMessage (/Users//Desktop/test/node_modules/puppeteer/lib/Connection.js:105:17)
at WebSocketTransport._ws.addEventListener.event (/Users//Desktop/test/node_modules/puppeteer/lib/WebSocketTransport.js:44:24)
at WebSocket.onMessage (/Users//Desktop/test/node_modules/ws/lib/event-target.js:120:16)
at WebSocket.emit (events.js:182:13)
at Receiver.receiverOnMessage (/Users//Desktop/test/node_modules/ws/lib/websocket.js:789:20)
at Receiver.emit (events.js:182:13)
at Receiver.dataMessage (/Users//Desktop/test/node_modules/ws/lib/receiver.js:422:14)
-- ASYNC --
at Frame.<anonymous> (/Users//Desktop/test/node_modules/puppeteer/lib/helper.js:111:15)
at Page.goto (/Users//Desktop/test/node_modules/puppeteer/lib/Page.js:674:49)
at Page.<anonymous> (/Users//Desktop/test/node_modules/puppeteer/lib/helper.js:112:23)
at getPic (/Users//Desktop/test/test.js:6:14)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:63995) 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:63995) [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.

Related

Twitter API V2.0 returning response with code 203

I'm building an app using the Twitter API V2.0 to get user info by username and it is returning me a code 203 response.
Code:
const { TwitterApi } = require('twitter-api-v2');
const twitterClient = new TwitterApi(tokens.BEARER);
const roClient = twitterClient.readOnly;
const user = await roClient.v2.userByUsername('plhery');
console.log(user);
Response:
(node:404) UnhandledPromiseRejectionWarning: Error: Request failed with code 403
at RequestHandlerHelper.createResponseError (C:\Users\IGOR\Documents\Code\Node\famoso_falou\node_modules\twitter-api-v2\dist\client-mixins\request-handler.helper.js:73:16)
at IncomingMessage.<anonymous> (C:\Users\IGOR\Documents\Code\Node\famoso_falou\node_modules\twitter-api-v2\dist\client-mixins\request-handler.helper.js:114:33)
at IncomingMessage.emit (events.js:412:35)
at endReadableNT (internal/streams/readable.js:1317:12)
at processTicksAndRejections (internal/process/task_queues.js:82:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:404) 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:404) [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.

(node:2136) UnhandledPromiseRejectionWarning: TypeError: browser

When try to run this url page this is the error i keep getting. Please Advice.
(node:2136) UnhandledPromiseRejectionWarning: TypeError: browser.newpage is not a function
at createPage (C:\Users\xolas\Downloads\Bot development\bot.js:7:32)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:2136) 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:2136) [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.
const puppeteer = require('puppeteer');
const product_url = "https://www.wilko.com/";
async function createPage() {
const browser = await puppeteer.launch({
headless: false
});
const page = await browser.newPage();
await page.goto(product_url);
}
createPage()

Troubles to show the browser using puppeteer from WSL2

i have troubles when i try to execute my script using puppeteer with the "headless" option set up in false to show the browser
Im Working with WSL2, here is my script
const puppeteer = require('puppeteer');
(async () => {
console.log('Start Scraping...');
const browser = await puppeteer.launch({ headless: false }); <-- Here's the problem
const page = await browser.newPage();
await page.goto('https://es.wikipedia.org/wiki/Node.js');
await browser.close();
console.log('End Scraping...');
})()
This is the output when i try to run it
Start Scraping...
(node:3057) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
[3068:3068:0429/043013.248542:ERROR:browser_main_loop.cc(1393)] Unable to open X display.
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
at onClose (/home/nilsonkr/courses/nodejs/foundaments/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:194:20)
at ChildProcess.<anonymous> (/home/nilsonkr/courses/nodejs/foundaments/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:185:79)
at ChildProcess.emit (events.js:327:22)
at Process.ChildProcess._handle.onexit (internal/child_process.js:277:12)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3057) 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:3057) [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.
When i set that value to true or just don't write it , it works, but i am still wondering what is the reason and how can i fix it ? to show the browser
i have already tried adding these flags but it didn't work either
const browser = await puppeteer.launch({headless: false,args: ['--no-sandbox', '--disable-setuid-sandbox']});

I Got A "UnhandledPromiseRejectionWarning" And A "DeprecationWarning" When Trying To Run The Code For The Bot, Any Solution To Fix This Problem?

Im Trying To Make A Discord Bot For A Server, I Entered "node ." into the terminal to run the bot and i got this error:
(node:17632) UnhandledPromiseRejectionWarning: Error: Incorrect login details were provided.
at WebSocketConnection.<anonymous> (C:\Users\Thela\code\node_modules\discord.js\src\client\ClientManager.js:48:41)
at Object.onceWrapper (events.js:300:26)
at WebSocketConnection.emit (events.js:210:5)
at WebSocketConnection.onClose (C:\Users\Thela\code\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:390:10)
at WebSocket.onClose (C:\Users\Thela\code\node_modules\ws\lib\event-target.js:124:16)
at WebSocket.emit (events.js:210:5)
at WebSocket.emitClose (C:\Users\Thela\code\node_modules\ws\lib\websocket.js:191:10)
at TLSSocket.socketOnClose (C:\Users\Thela\code\node_modules\ws\lib\websocket.js:850:15)
at TLSSocket.emit (events.js:215:7)
at net.js:658:12
(node:17632) 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: 2)
(node:17632) [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.
In The Terminal.
This Is The index.js file:
const Discord = require('discord.js');
const config = require('./config.json');
const client = new Discord.Client();
client.once('ready', () => {
console.log('Ready!');
});
client.on('message', message => {
if (message.content === '!ping') {
message.channel.send('Pong.');
}
});
client.login(config.token);
And Here Is The config.json:
{
"prefix": "!",
"token": "7F2x4ct0AMuBeJKZUGmtIz_RZVKve-N4"
}
i realised i didnt put the full error
oops
What you are using as "token" is actually your client secret, the token should look something like this:
NjQwOTM2MjE3MTg5Mjg1ODg4.XcGSOQ.3dgX4GbvEDZKYHnCb6nJORuvL1w

How do ES6 async/await work with Observables and streams when they fail?

So, I've been trying to replicate this code:
https://github.com/sindresorhus/np/blob/370ef638344ab7115c956b75dc2823850084da39/index.js#L16
And it works. However, if the promise fails, I get an "unhandled promise" warning. Where does the catch statement belong on something like this if at all? Is there a better way to source information like this?
(node:48454) UnhandledPromiseRejectionWarning: Error: Command failed: np patch --no-cleanup
at makeError (/Users/daghassi/git/build/node_modules/execa/index.js:172:9)
at Promise.all.then.arr (/Users/daghassi/git/build/node_modules/execa/index.js:277:16)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:160:7)
(node:48454) 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: 5)
(node:48454) [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.
(node:48454) UnhandledPromiseRejectionWarning: Error: Command failed: np patch --no-cleanup
at makeError (/Users/daghassi/git/build/node_modules/execa/index.js:172:9)
at Promise.all.then.arr (/Users/daghassi/git/build/node_modules/execa/index.js:277:16)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:160:7)
(node:48454) 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: 6)
(node:48454) UnhandledPromiseRejectionWarning: Error: Command failed: np patch --no-cleanup
Just put a try/catch block around your await call. This should be the same as if you would call a Promise and provide a catch function.
So if you have a function which getPromise() which returns a promise, you simply do this.
async function asyncFunction() {
try {
const result = await getPromise();
} catch (error) {
// Add your error handling code here
}
}

Categories