Puppeteer Protocol error (Page.navigate): - node.js

Error: Protocol error (Page.navigate): Cannot navigate to invalid URL
at Promise (/home/microservice/node_modules/puppeteer/lib/Connection.js:183:56)
at new Promise ()
at CDPSession.send (/home/microservice/node_modules/puppeteer/lib/Connection.js:182:12)
at navigate (/home/microservice/node_modules/puppeteer/lib/FrameManager.js:119:39)
at FrameManager.navigateFrame (/home/microservice/node_modules/puppeteer/lib/FrameManager.js:96:7)
at Frame.goto (/home/microservice/node_modules/puppeteer/lib/FrameManager.js:407:37)
at Frame. (/home/microservice/node_modules/puppeteer/lib/helper.js:111:23)
at Page.goto (/home/microservice/node_modules/puppeteer/lib/Page.js:629:49)
at Page. (/home/microservice/node_modules/puppeteer/lib/helper.js:111:23)
at module.exports (/home/microservice/node_modules/htmltopdf-puppeteer/index.js:19:16)\n at process._tickCallback (internal/process/next_tick.js:68:7)

Use http to access the url over network or use file:/// to access the local file
const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox'] })
const page = await browser.newPage();
await page.goto("http://google.com");
or
await page.goto("file:///C:/ec2-user/report.html");

Related

Invalid chai property: browser when trying to run a test on puppeteer

i'm trying to run a test on a website using puppeteer and i want to use 'expect' assertion from chai but i keep getting this error and that my test failed.
const puppeteer = require('puppeteer')
const expect = require('chai').expect
describe('My First Puppeteer Test ', () => {
it( 'should launch the browser', async function() {
const browser = await puppeteer.launch({
headless: false,
slowMo: 10,
devtools: false,
defaultViewport: null,
args: ['--start-maximized']
})
const page = await browser.newPage()
await page.goto('http://example.com')
const title = await page.title()
const url = page.url()
const text = await page.$eval('h1', Element => Element.textContent)
const count = await page.$$eval('p', Element => Element.length)
expect(title).to.be.a('string', 'Example Domain')
expect(url).to.include('example.com')
expect(text).to.browser.a('string', 'example domain')
expect(count).to.equal(2)
await browser.close()
})
})
what's wrong with it?
do i need to add something in order for this to work?
this is the message i get from terminal
My First Puppeteer Test
1) should launch the browser
0 passing (1s)
1 failing
1) My First Puppeteer Test
should launch the browser:
Error: Invalid Chai property: browser
at Object.proxyGetter [as get] (node_modules\chai\lib\chai\utils\proxify.js:78:17)
at Context.<anonymous> (tests\example.test.js:25:24)
at runNextTicks (node:internal/process/task_queues:61:5)
at listOnTimeout (node:internal/timers:528:9)
at processTimers (node:internal/timers:502:7)

Unable to load script on puppeteer AWS Lambda

Im trying to add an external javascript file to puppeteer on Lambda.
const chromium = require('chrome-aws-lambda');
const browser = await chromium.puppeteer.launch({
....
});
const webPage = await browser.newPage();
await webPage.goto(url, { waitUntil: 'networkidle2', timeout: 0 });
await webPage.addScriptTag({
url: `https://code.jquery.com/jquery-3.6.0.min.js`,
});
But unable to load the script
ERROR Error: Loading script from https://code.jquery.com/jquery-3.6.0.min.js failed
at DOMWorld.addScriptTag (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/common/DOMWorld.js:171:23)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async I (/var/task/src/functions/preparePages/handler.js:20:3689)
at async Runtime.E [as handler] (/var/task/src/functions/preparePages/handler.js:20:5400)

Puppeteer Fails to launch the browser

After creating a directory with an index.js file with the following code:
const puppeteer = require('puppeteer');
async function main() {
const browser = await puppeteer.launch({
headless: false,
args: ['--no-sandbox']
});
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({
path: 'example.png'
});
await browser.close();
}
// Start the script
main();
and then running npm init, and npm install puppeteer, the following error is returned:
node index.js
/mnt/c/Users/trgre/OneDrive/Desktop/puppeteer
test/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:214
reject(new Errors_js_1.TimeoutError(`Timed out after ${timeout} ms while trying to
connect to the browser! Only Chrome at revision r${preferredRevision} is guaranteed to
work.`));
^
TimeoutError: Timed out after 30000 ms while trying to connect to the browser! Only Chrome at
revision r901912 is guaranteed to work.
at Timeout.onTimeout (/mnt/c/Users/trgre/OneDrive/Desktop/puppeteer
test/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:214:20)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7)
Node.js v17.1.0
Any ideas on what to do in order to run a puppeteer program, I am on windows using Ubuntu 20?

puppeteer page.authenticate https proxy not work

Running puppeteer with Proxy authentication failing.
Puppeteer version: 1.8
Platform / OS version:MacOS 10.13.6
Node.js version:v10.9.0
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless:false,
ignoreHTTPSErrors:true,
devtools:true,
timeout:3000,
args: ['--no-sandbox','--proxy-server=xxx:xxx']
});
const user='xxx';
const password='xxx';
const page = await browser.newPage();
// await page.setExtraHTTPHeaders({
// 'Proxy-Authorization': 'Basic ' + Buffer.from(`${user}:${password}`).toString('base64'),
// });
await page.authenticate({username:user, password:password});
await page.goto('https://www.apple.com/');
let title = await page.title();
console.log("title:" + title);
await browser.close();
})();
got error:
(node:5858) UnhandledPromiseRejectionWarning:
Error: net::ERR_TUNNEL_CONNECTION_FAILED at https://www.apple.com/
at navigate (/xxx/node_modules/_puppeteer#1.8.0#puppeteer/lib/Page.js:622:37)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:5858) 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)
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless:false,
ignoreHTTPSErrors:true,
devtools:true,
timeout:3000,
args: ['--no-sandbox','--proxy-server=https=xxx:xxx']
});
const user='xxx';
const password='xxx';
const page = await browser.newPage();
await page.authenticate({username:user, password:password});
await page.goto('https://www.apple.com/');
let title = await page.title();
console.log("title:" + title);
await browser.close();
})();
For me it worked by adding https between the proxy server argument.
Within your args to launch puppeteer,
You can also try this format:
--proxy-server=http=xxx:xxxx
OR
Have you tried setting proxy like this '--proxy-server=https=MyWorkingProxyIP:Port' according to docs https://www.chromium.org/developers/design-documents/network-settings ?

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