try {
await page.waitforselector('#element', { timeout: 1000 });
// do what you have to do here
} catch (e) {
console.log('element probably not exists');
here is a fully working example:
const puppeteer = require('puppeteer');
const html = `
<div id="element">element inner html</div>
</body>
</html>`;
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newpage();
await page.goto(`data:text/html,${html}`);
try {
await page.waitforselector('#element-not-exists', { timeout: 1000 });
const element = await page.$('#element-not-exists');
console.log(await (await element.getproperty('innerhtml')).jsonvalue());
} catch (e) {
console.log('element probably not exists');
await browser.close();
})();
when i was trying to scrape some pages, i was facing similar issue as default timeout was 30000 ms i.e. 30 seconds and page was taking longer than 30 seconds to load, so basically there was 2 main issues.
script was not getting killed after nodejs throwing
page.waitfornavigation() timeout error, so it was keeping mysql
connection active to the server and new connections was being
created by cronjob and they were all in sleep condition.
page was not being scraped so needed to increase timeout.
here is my final code.
const puppeteer = require('puppeteer');
var mysql = require('mysql');
var mysql_con = mysql.createconnection({
host: "",
user: "",
password: "",
database: ""
//connect to mysql
mysql_con.connect(function(err) {
if (err) throw err;
console.log("connected! to mysql");
(async () => {
const args = [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-infobars',
'--window-position=0,0',
'--ignore-certifcate-errors',
'--ignore-certifcate-errors-spki-list',
'--ignorehttpserrors=true',
'--user-agent="mozilla/5.0 (linux; android 6.0.1; nexus 5x build/mmb29p) applewebkit/537.36 (khtml, like gecko) chrome/w.x.y.z‡ mobile safari/537.36 (compatible; googlebot/2.1; +http://www.google.com/bot.html)"'
const browser = await puppeteer.launch({ args });
const page = await browser.newpage();
// configure the navigation timeout to 2 minutes, becuase sometimes site is too busy
await page.setdefaultnavigationtimeout(120000);
try {
// now you can go wherever you want
const response = await page.goto('https://www.example.com/');
//print http status code
console.log(response.status());
//do mysql related stuff here
//close mysql connection
mysql_con.end();
} catch (e) {
console.log('cant load the page, maybe server is busy : ' + e);
//close mysql connection
mysql_con.end();
await browser.close();
//double tap to die script
process.exit();
// then when you're done, just close
await browser.close();
})();
here we fixed 2 issues.
first issue by using try and catch block and killing script/ending mysql connection in catch block.
increasing page timeout from 30 seconds default to 2 minutes.
page.setdefaultnavigationtimeout(120000);
you can use waitfornavigation method after button click and attach a catch block like below to handle navigation timeout error in case value entered is incorrect and navigation does not happens
page.waitfornavigation({waituntil:"domcontentloaded"}).catch(error => {
// handler code here
the try catch
block is a solution.
however, on the accepted answer all errors are silenced!!
you should only catch the puppeteer timeout errors.
try {
await page.waitforselector('.foo');
} catch (e) {
if (e instanceof puppeteer.errors.timeouterror) {
// do something if this is a timeout.
references: https://devdocs.io/puppeteer/
Puppeteer timeout error while using waitForSelector()
timeout error with navigation and waitForSelector() in puppeteer irrespective of timeout value
Trying to crawl a website using puppeteer but getting a timeout error
Error Handling by display static HTML page
Warp function outside with error handling
Error Handling on yEnc in nodejs
Error handling with Node Redis client
Multiple promises in Node with error handling
NodeJS request and error handling
Problems with AWS Load Balancer Timeout and No Error in Node/Express
How to use global error handling code in node.js for entire api call
Redis timeout error
Mongoose error handling
HTTPS module error handling when disconnecting from internet in Node.js
What is causing Error TypeError: text is not iterable? - Web scraper Puppeteer NodeJs
Unable to bypass some kind of fingerprinting system to a login page with Puppeteer / NodeJS
How to save web page content as mp4 using puppeteer in nodejs
Handling post request for 2 forms in one page using express
Angular Mat-Form Causing Web Page to Crash Without Error Code
NextJS Error Page Custom CSS is Not Working
i am not getting req.session in error handling middleware my Node and express app?
Puppeteer blank page when accesing URL
Overriding express general error handling
Getting the error "TypeError: Page is not a constructor."
I am building an electron app and I am trying to ensure that if one url is unavailable a second one is attempted before my default error page
Puppeteer cannot goto web page to get selector
Error occurred prerendering page "/404" and Error for page /_error: pages with `getServerSideProps` can not be exported
Puppeteer imported function error : this._pageBindings.get(...) is not a function
Express 4 - Custom error handling middleware is not being called
Error handling in Express after axios call
More Query from same tag
I cannot connect to SQL server using Node.JS server
NLP Sentiments: Giving wrong result when using negative word in positive way
How can I debug queries sent via mysql2?
How set a value within connection.query function nodejs
How to retrieve attribute values from n number of child elements using puppeteer?
node.js : Unhandled promise rejection causes nodemon to not record error
Path to Application Support on Mac in Node.js
How to get data from the backend that needs authorization using React
Duplicate mongodb objects params but only one gets shown? Last digits get rounded?
Service can't find dependency because of another services injection
mysql insert at the same time?
NodeJs Swagger Typescript controller not found
"Error: Failed to lookup view "error" in views directory "E:\tozip\views" "
How to Resolve Error: Watchman error: watchman::RootResolveError:
NodeJS http.request end processing before data processes
nodejs stream csv data into mongoDB
What is the best way to authenticate user with node and angularjs?
errno: -2, code: 'ENOENT', syscall: 'open',
AJAX, Fetch API, or GET-request via browser — which request technique should be used for a file download?
Iterate over an array using async js and executing a function inside callback to return a single result
trying to build nodejs application written in typescrip return error with socket.io
What is the correct way to read a property of an object wrapped in a promise? (Getting TypeError)
Pass angularjs variable to nodejs function from view
Files written with fs.writeFile have extra new lines compared to console.log. Why?
Set permission for Role to none
How to proxy/stream HTTPS request using Express/got?
Node.js and Socket.io app with Apache Cordova not working in android
How to properly stub out request-promise in a mocha unit test using sinon?
Disable SSL Trusted Peer requirement in specific express router
Not able to debug node js app from browser