简体   繁体   English

在 puppeteer 中按 Enter 按钮

[英]Pressing Enter button in puppeteer

Pressing enter in puppeteer doesn't seem to have any effect.在 puppeteer 中按 enter 似乎没有任何效果。 However, when I press other keys, it does what it should.但是,当我按下其他键时,它会做它应该做的事情。 This works:这有效:

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 来确定。

I've used page.keyboard.press('Enter');我用过page.keyboard.press('Enter'); usually :) Works for me.通常:) 对我有用。

Have a look at the documentation here .看看这里的文档。 I think you should use .keyboard before .press for it to work properly.我认为你应该使用.keyboard之前.press它才能正常工作。

page.keyboard.press(): page.keyboard.press():

You can use page.keyboard.press() to simulate pressing the enter key.您可以使用page.keyboard.press()来模拟按下 Enter 键。 Any of the following options should work:以下任何选项都应该有效:

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()的组合在按下 Enter 键之前将焦点放在一个元素上:

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');
await page.type(String.fromCharCode(13));

Using this site I noticed that page.type dispatches beforeinput and input events, but page.press doesn't.使用这个网站我注意到page.type调度beforeinputinput事件,但page.press没有。 This is probably a bug, but fortunately sending the enter keycode (13) seems to work, so we can work around it for now.这可能是一个错误,但幸运的是发送输入密钥代码 (13) 似乎有效,所以我们现在可以解决它。

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', 'test.anonymous.user@gmail.com');
  //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();

await page.keyboard.press('Enter'); This work for me这对我有用

I have used await page.keyboard.press('Enter');我用过await page.keyboard.press('Enter'); in my code and it worked perfectly fine for me.在我的代码中,它对我来说非常好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM