Javascript 在 puppeteer 中按 Enter 按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46442253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pressing Enter button in puppeteer
提问by elena
Pressing enter in puppeteer doesn't seem to have any effect. However, when I press other keys, it does what it should. This works:
在 puppeteer 中按 Enter 似乎没有任何效果。但是,当我按下其他键时,它会做它应该做的事情。这有效:
await page.press('ArrowLeft');
This doesn't:
这不会:
await page.press('Enter');
This is how the input looks like:
这是输入的样子:
Any ideas?
有任何想法吗?
EDIT: I've also tried page.keyboard.down & page.keyboard.up to be sure.
编辑:我也试过 page.keyboard.down & page.keyboard.up 可以肯定。
采纳答案by BlackCap
await page.type(String.fromCharCode(13));
Using this siteI noticed that page.typedispatches beforeinputand inputevents, but page.pressdoesn't. This is probably a bug, but fortunately sending the enter keycode (13) seems to work, so we can work around it for now.
使用这个站点,我注意到page.type调度beforeinput和input事件,但page.press没有。这可能是一个错误,但幸运的是发送输入密钥代码 (13) 似乎有效,所以我们现在可以解决它。
回答by kaiak
回答by Grant Miller
page.keyboard.press():
page.keyboard.press():
You can use page.keyboard.press()to simulate pressing the enter key. Any of the following options should work:
您可以使用page.keyboard.press()来模拟按下回车键。以下任何选项都应该有效:
await page.keyboard.press('Enter'); // Enter Key
await page.keyboard.press('NumpadEnter'); // Numeric Keypad Enter Key
await page.keyboard.press('\n'); // Shortcut for Enter Key
await page.keyboard.press('\r'); // Shortcut for Enter Key
elementHandle.press():
elementHandle.press():
Additionally, you can use use a combination of page.$()and elementHandle.press()to focus on an element before pressing enter:
此外,还可以使用使用的组合page.$(),并elementHandle.press()按下进入前一个元素上的焦点:
await (await page.$('input[type="text"]')).press('Enter'); // Enter Key
await (await page.$('input[type="text"]')).press('NumpadEnter'); // Numeric Keypad Enter Key
await (await page.$('input[type="text"]')).press('\n'); // Shortcut for Enter Key
await (await page.$('input[type="text"]')).press('\r'); // Shortcut for Enter Key
page.type():
页面类型():
Furthermore, you can use page.type():
此外,您可以使用page.type():
await page.type(String.fromCharCode(13));
page.keyboard.type():
page.keyboard.type():
Likewise, you can use page.keyboard.type():
同样,您可以使用page.keyboard.type():
await page.keyboard.type(String.fromCharCode(13));
page.keyboard.sendCharacter():
page.keyboard.sendCharacter():
Another alternative method would be to use the page.keyboard.sendCharacter()method:
另一种替代方法是使用该page.keyboard.sendCharacter()方法:
await page.keyboard.sendCharacter(String.fromCharCode(13));
page.keyboard.down() / page.keyboard.up():
page.keyboard.down() / page.keyboard.up():
You can also use a combination of page.keyboard.down()and page.keyboard.up():
您还可以使用的组合page.keyboard.down()和page.keyboard.up():
// Enter Key
await page.keyboard.down('Enter');
await page.keyboard.up('Enter');
// Shortcut for Enter Key
await page.keyboard.down('NumpadEnter');
await page.keyboard.up('NumpadEnter');
// Shortcut for Enter Key
await page.keyboard.down('\n');
await page.keyboard.up('\n');
// Shortcut for Enter Key
await page.keyboard.down('\r');
await page.keyboard.up('\r');
回答by Yordan Georgiev
short answer it might be a good idea to attach to the input control first :
简短的回答首先附加到输入控件可能是个好主意:
await page.type('#inp_srch_box', 'add');
await page.type('#inp_srch_box',String.fromCharCode(13));
Long answer ...
长答案...
cat package.json
{
"name": "test-open-login",
"version": "0.7.9",
"description": "an example test to test the login of the qto app",
"main": "test-open-login.test.js",
"scripts": {
"test": "mocha --recursive test",
"server": "http-server src"
},
"license": "BSD",
"devDependencies": {
"mocha": "5.0.1",
"puppeteer": "^2.1.0"
},
"dependencies": {
"chai": "^4.2.0",
"http-server": "^0.12.1",
"mocha": "5.0.1"
}
}
cat test/test-login.spec.js
const puppeteer = require('puppeteer');
async function main(){
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.setViewport({width: 1200, height: 720})
await page.goto('https://qto.fi/qto/login', { waitUntil: 'networkidle0' });
// wait until
await page.type('#email', '[email protected]');
//await page.type('#pass', 'secret');
// click and wait for navigation
await page.waitFor(1000);
//await frame.waitFor(1000);
await Promise.all([
page.click('#butLogin'),
page.waitForNavigation({ waitUntil: 'networkidle0' }),
]);
await page.type('#inp_srch_box', 'add');
await page.type('#inp_srch_box',String.fromCharCode(13));
}
main();
回答by theB
Its page.keyboard.press()
它的page.keyboard.press()
Reference: https://pptr.dev/#?product=Puppeteer&version=v3.0.0&show=api-keyboardpresskey-options
参考:https: //pptr.dev/#?product=Puppeteer&version=v3.0.0&show=api-keyboardpresskey-options
Find keynames and keycodes at https://github.com/puppeteer/puppeteer/blob/master/src/USKeyboardLayout.js
在https://github.com/puppeteer/puppeteer/blob/master/src/USKeyboardLayout.js 中查找键名和键码
回答by Amir Sikandar
await page.keyboard.press('Enter'); This work for me
等待 page.keyboard.press('Enter'); 这对我有用


