Page.goto() always causes a Navigation Timeout - but i can get screenshot somehow
See original GitHub issueSteps to reproduce
- Puppeteer version: 1.2.0
- Platform / OS version: Windows 7 x64
- Node.js version: 8.11.1
What steps will reproduce the problem? I am trying this code:
// <-- add event on top of file
process.on("unhandledRejection", (reason, p) => {
console.error("Unhandled Rejection at: Promise", p, "reason:", reason);
// browser.close(); // <-- no need to close the browser here
});
const puppeteer = require('puppeteer');
async function getPic() {
try{ // <-- wrap the whole block in try catch
const browser = await puppeteer.launch(/*{headless: false}*/);
const page = await browser.newPage();
await page.setViewport({width: 1000, height: 500}); // <-- add await here so it sets viewport after it creates the page
//await page.goto('https://www.google.com'); //Old way of doing. It doesn't work for some reason...
page.goto('https://www.google.com/'); // async
// wait for either of events to trigger
await Promise.race([
page.waitForNavigation({waitUntil: 'domcontentloaded'}),
page.waitForNavigation({waitUntil: 'load'})
]);
await page.screenshot({path: 'pic.png'});
await browser.close(); // <-- close browser after everything is done
} catch (error) {
console.log(error);
}
}
getPic();
I request a pic from the page loaded for testing purposes.
With code as it is, i get pic and this error:
Unhandled Rejection at: Promise Promise {
<rejected> Error: Navigation Timeout Exceeded: 30000ms exceeded
at Promise.then (C:\...\pupet test\node_modules\pupp
eteer\lib\NavigatorWatcher.js:71:21)
at <anonymous> } reason: Error: Navigation Timeout Exceeded: 30000ms exceede
d
at Promise.then (C:\...\pupet test\node_modules\pupp
eteer\lib\NavigatorWatcher.js:71:21)
at <anonymous>
When i remove the process.on() code in the beginning, i get no pic and this error message:
at Promise.then (C:\...\pupet test\node_modules\puppeteer\lib\NavigatorWatcher.js:71:21)
at <anonymous>
(node:5548) UnhandledPromiseRejectionWarning: Error: Navigation Timeout Exceeded
: 30000ms exceeded
at Promise.then (C:\...\pupet test\node_modules\puppeteer\lib\NavigatorWatcher.js:71:21)
at <anonymous>
(node:5548) 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:5548) [DEP0018] DeprecationWarning: Unhandled promise rejections are depre
cated. In the future, promise rejections that are not handled will terminate the
Node.js process with a non-zero exit code.
When process.on() IS there but instead i use await
page.goto('https://www.google.com');
INSTEAD OF
page.goto('https://www.google.com/'); // async
// wait for either of events to trigger
await Promise.race([
page.waitForNavigation({waitUntil: 'domcontentloaded'}),
page.waitForNavigation({waitUntil: 'load'})
]);
i don’t get the picture and i get this:
Error: Navigation Timeout Exceeded: 30000ms exceeded
at Promise.then (C:\...\pupet test\node_modules\pupp
eteer\lib\NavigatorWatcher.js:71:21)
at <anonymous>
Finally, when i remove process.on() and i use await
page.goto('https://www.google.com');
INSTEAD OF
page.goto('https://www.google.com/'); // async
// wait for either of events to trigger
await Promise.race([
page.waitForNavigation({waitUntil: 'domcontentloaded'}),
page.waitForNavigation({waitUntil: 'load'})
]);
i don’t get the picture and i get this:
Error: Navigation Timeout Exceeded: 30000ms exceeded
at Promise.then (C:\...\pupet test\node_modules\pupp
eteer\lib\NavigatorWatcher.js:71:21)
at <anonymous>
In conclusion, whenever i don’t use process.on() i don’t have my code executed.
But when i do use process.on() i get the screenshot but i still get the error. Now i know that the ‘unhandledRejection’ event is emitted whenever a Promise is rejected and no error handler is attached to the promise within a turn of the event loop.
Now, when i use :
page.goto('https://www.google.com/').catch(error => console.log("this isn't handled by the try catch since you don't await it", error));
for troubleshooting purposes, i get:
this isn't handled by the try catch since you don't await it Error: Navigation T imeout Exceeded: 30000ms exceeded at Promise.then (C:\Users\User1\Desktop\fb proj\pupet test\node_modules\pupp eteer\lib\NavigatorWatcher.js:71:21) at <anonymous>
Which means page.goto() is the culprit. No matter what i try i cannot seem to make it work. It appears to be completely illogical, how come it throws an error but still manages to get the pic?
If i try stuff like:
const puppeteer = require('puppeteer');
async function getPic() {
try { // <-- wrap the whole block in try catch
const browser = await puppeteer.launch(/*{headless: false}*/);
const page = await browser.newPage();
await page.setViewport({width: 1000, height: 500}); // <-- add await here so it sets viewport after it creates the page
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36');
await page.setExtraHTTPHeaders({
'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8'
});
await page.goto('https://www.google.com/', {waitUntil: 'load'})
await page.screenshot({path: 'pic.png'});
await browser.close(); // <-- close browser after everything is done
} catch (error) {
console.log(error);
}
}
getPic();
Then i just get the error without any picture.
I should also add, that i was contacted by the guy who wrote the unhandledRejection for node and told me that i shouldn’t put it in my code. But wihtout it… it doesn’t work at all!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:7
- Comments:7 (2 by maintainers)
Top GitHub Comments
For anyone else having a similar issue, I solved it by adding a protocol (http://) to the url, otherwise it timesout when creating a pdf (but works with
headless: false
).I have the same problem, have you resolve it?