How to fix Puppeteer 'Error: Unknown key: "tab"'
Problem:
You want to emulate a tab key
press using
await page.keyboard.press("tab");
but you get an error message like
(node:30594) UnhandledPromiseRejectionWarning: Error: Unknown key: "tab"
at assert (/home/uli/dev/myproject/node_modules/puppeteer/lib/helper.js:270:11)
at Keyboard._keyDescriptionForString (/home/uli/dev/myproject/node_modules/puppeteer/lib/Input.js:96:5)
at Keyboard.down (/home/uli/dev/myproject/node_modules/puppeteer/lib/Input.js:44:30)
at Keyboard.<anonymous> (/home/uli/dev/myproject/node_modules/puppeteer/lib/helper.js:112:23)
at Keyboard.press (/home/uli/dev/myproject/node_modules/puppeteer/lib/Input.js:178:16)
at Keyboard.<anonymous> (/home/uli/dev/myproject/node_modules/puppeteer/lib/helper.js:112:23)
at /home/uli/dev/myproject/test.js:8:23
at processTicksAndRejections (internal/process/task_queues.js:85:5)
(node:30594) 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: 2)
(node:30594) [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.
Solution
You need to use Tab
, not tab
! The T
needs to be uppercase!
Use
await page.keyboard.press("Tab");
Full example:
// Minimal puppeteer example
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({defaultViewport: {width: 1920, height: 1080}});
const page = await browser.newPage();
await page.goto('https://techoverflow.net', {waitUntil: 'domcontentloaded'});
// Press tab 10 times (effectively scrolls down on techoverflow.net)
for (let i = 0; i < 10; i++) {
await page.keyboard.press("Tab");
}
// Screenshot to verify result
await page.screenshot({path: 'screenshot.png'});
// Cleanup
await browser.close();
})();