Puppeteer beheben: 'Error: Unknown key: "tab"'
English
Deutsch
Problem:
Sie möchten einen tab-Tastendruck emulieren mit
puppeteer_unknown_key_tab_fix.js
await page.keyboard.press("tab");aber Sie erhalten eine Fehlermeldung wie
puppeteer_unknown_key_tab_error.txt
(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.Lösung
Sie müssen Tab verwenden, nicht tab! Das T muss großgeschrieben sein!
Verwenden Sie
puppeteer_tab_press_fix.js
await page.keyboard.press("Tab");Vollständiges Beispiel:
puppeteer_tab_full_example.js
// Minimales Puppeteer-Beispiel
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'});
// Tab 10-mal drücken (führt auf techoverflow.net effektiv zu Scrollen nach unten)
for (let i = 0; i < 10; i++) {
await page.keyboard.press("Tab");
}
// Screenshot zur Verifikation des Ergebnisses
await page.screenshot({path: 'screenshot.png'});
// Aufräumen
await browser.close();
})();Check out similar posts by category:
Javascript
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow