繁体   English   中英

在 puppeteer 中按 Enter 按钮

[英]Pressing Enter button in puppeteer

在 puppeteer 中按 enter 似乎没有任何效果。 但是,当我按下其他键时,它会做它应该做的事情。 这有效:

await page.press('ArrowLeft');

这不会:

await page.press('Enter');

这是输入的样子:

在此处输入图像描述

有任何想法吗?

编辑:我也试过 page.keyboard.down 和 page.keyboard.up 来确定。

我用过page.keyboard.press('Enter'); 通常:) 对我有用。

看看这里的文档。 我认为你应该使用.keyboard之前.press它才能正常工作。

page.keyboard.press():

您可以使用page.keyboard.press()来模拟按下 Enter 键。 以下任何选项都应该有效:

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():

此外,您可以使用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()

await page.type(String.fromCharCode(13));

page.keyboard.type():

同样,您可以使用page.keyboard.type()

await page.keyboard.type(String.fromCharCode(13));

page.keyboard.sendCharacter():

另一种替代方法是使用page.keyboard.sendCharacter()方法:

await page.keyboard.sendCharacter(String.fromCharCode(13));

page.keyboard.down() / 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));

使用这个网站我注意到page.type调度beforeinputinput事件,但page.press没有。 这可能是一个错误,但幸运的是发送输入密钥代码 (13) 似乎有效,所以我们现在可以解决它。

简短的回答首先附加到输入控件可能是个好主意:

    await page.type('#inp_srch_box', 'add');
    await page.type('#inp_srch_box',String.fromCharCode(13));

长答案...

  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'); 这对我有用

我用过await page.keyboard.press('Enter'); 在我的代码中,它对我来说非常好。

暂无
暂无

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

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