How to open link with using puppeter wsendpoint - node.js

I try to connect browser with wsendpoint like this :
(async () => {
const browser = await puppeteer.connect({browserWSEndpoint: 'ws://localhost:9222/devtools/page/eedbdd50-cd25-472d-a701-a0302d9ffe2f'});
const page = await browser.newPage();
await page.goto('https://www.google.com.tr');
})();
this code can open new tab but do not go to link. It give error:
(node:2022) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Protocol error (Performance.enable): 'Performance.enable' wasn't found undefined
(node:2022) [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.

Try this:
const puppeteer = require('puppeteer');
puppeteer.connect({
browserWSEndpoint: 'your endpoint...'
}).then(async browser => {
const page = await browser.newPage();
await page.goto('https://www.google.com.tr');
});

Related

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?

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.
}
})();

nodejs program to run puppyteerjs fails

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

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.

UnhandledPromiseRejectionWarning: Error: Protocol error (Network.setCookies): Target closed - Can't set cookies

Currently I'm getting an issue where Puppeteer crashes upon using the setCookies method. I'm currently using Puppeteer v 1.4.0 (latest version as of writing this) as well as the Chromium version that is bundled with Puppeteer, and here is the code that is giving me trouble:
const puppeteer = require('puppeteer');
const moment = require('moment');
(async () => {
const browser = await puppeteer.launch(
{
headless: false
}
);
const page = await browser.newPage();
await page.goto('https://google.com');
const currentUrl = await page.url();
await browser.close();
const browser1 = await puppeteer.launch(
{
headless: false
}
);
const page1 = await browser1.newPage();
const cookie = await currentUrl.split("/");
await page1.setCookie({
'name': 'samplename',
'value': cookie[0],
'domain': 'sampledomain',
'path': cookie[0] + '/' + cookie[0] + '/' + cookie[0],
'expires': moment().add(21, 'days').valueOf(),
'httpOnly': false,
'secure': true,
'sameSite': "Lax"
});
await page1.goto(currentUrl);
})();
and here is the error message
(node:64704) UnhandledPromiseRejectionWarning: Error: Protocol error (Network.setCookies): Target closed.
at Promise (/Users/pc/Desktop/Shopify Bot/node_modules/puppeteer/lib/Connection.js:200:56)
at new Promise (<anonymous>)
at CDPSession.send (/Users/pc/Desktop/Shopify Bot/node_modules/puppeteer/lib/Connection.js:199:12)
at Page.setCookie (/Users/pc/Desktop/Shopify Bot/node_modules/puppeteer/lib/Page.js:320:26)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:182:7)
(node:64704) 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:64704) [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've looked into the issue on my own for a while, and multiple sources seem to say that not actually executing async is the problem, however, I believe I am running everything in async (however, as this is my first time doing anything async on NodeJS, I could be making critical errors in judgement). I've tried verifying my Chromium and uninstalling + reinstalling Puppeteer, but nothing seems to avail.
The reason you're gettning an error is in this line:
'path': cookie[0] + '/' + cookie[0] + '/' + cookie[0],
It resolves into https:/https:/https: which is not a valid value for this property.
Try setting path to '/' or just do not set this property and it's going to work just fine.
You can find more reading on how to use path here and here.

Resources