'How to handle waitForSelector when selector is not found

I'm trying to wait for a selector. When the selector is not available, which can happen I would expect to catch that error in the .catch() and be able to proceed with my program. Instead I get the UnhandledPromiseRejectionWarning error.

How can I write a program that can handle selectors that is not found or times out?

Code

await page.waitForSelector('#step3 > div > div > div > div.time-selection-wrapper > div.v-card.v-sheet.theme--light > div > div > div.v-picker__title.primary-color > div > div.v-picker__title__btn.v-date-picker-title_custom1.v-picker__title__btn--active > div', {timeout: 5000})
    .then(async () => {
        console.log("Entered THEN")
    })
    .catch((e) => {
        if (e instanceof puppeteer.errors.TimeoutError) {
            console.log("Timeout")
        }
    })

Output

Timeout (node:9540) UnhandledPromiseRejectionWarning: Error: Node is either not clickable or not an HTMLElement at ElementHandle.clickablePoint (/home/john/git/myproject/node_modules/puppeteer/lib/cjs/puppeteer/common/JSHandle.js:412:19) (node:9540) 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:9540) [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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source