77

Pressing enter in puppeteer doesn't seem to have any effect. 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:

enter image description here

Any ideas?

EDIT: I've also tried page.keyboard.down & page.keyboard.up to be sure.

16
  • Perhaps try 'Accept'? Commented Sep 27, 2017 at 7:53
  • I've tried await page.press('Accept') but no result :(
    – elena
    Commented Sep 27, 2017 at 7:57
  • Follow this link keys-special, and maybe, it is not page.press('Enter') not works, maybe you catch page just listen to mouseup event.
    – Carson
    Commented Sep 27, 2017 at 7:58
  • did u try await page.press('Return')
    – Carson
    Commented Sep 27, 2017 at 8:00
  • 2
    I am also having this problem. I am running puppeteer with puppeteer.launch({headless: false}); so I can see what's going on, and ArrowLeft works, but Enter doesn't. I checked the API documentation, and it clearly says that page.press("Enter") is the way to do this, but it doesn't work for me.
    – BlackCap
    Commented Sep 28, 2017 at 19:29

7 Answers 7

100

I've used 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.

3
  • Can you elaborate more on your answer?
    – Sid
    Commented Nov 20, 2017 at 10:41
  • Not sure what to elaborate. I think you should add .keyboard before .press for it to work. At least this is how I simulate pressing Enter in my applications.
    – kaiak
    Commented Nov 20, 2017 at 11:04
  • 1
    @Sid The answer below, which was added a year after your comment, has all the elaboration you'd probably want....
    – ggorlen
    Commented Aug 3, 2021 at 0:03
65

page.keyboard.press():

You can use page.keyboard.press() to simulate pressing the enter key. 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():

Additionally, you can use use a combination of page.$() and elementHandle.press() to focus on an element before pressing 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():

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

page.keyboard.type():

Likewise, you can use page.keyboard.type():

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

page.keyboard.sendCharacter():

Another alternative method would be to use the page.keyboard.sendCharacter() method:

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

page.keyboard.down() / page.keyboard.up():

You can also use a combination of page.keyboard.down() and 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');
1
  • the normal page.keyboard.press('Enter') doesn't work in my case, but await (await page.$(selector).press('Enter'); WORKS.
    – Woden
    Commented Nov 4, 2022 at 0:42
49
await page.type(String.fromCharCode(13));

Using this site I noticed that page.type dispatches beforeinput and input events, but page.press doesn't. This is probably a bug, but fortunately sending the enter keycode (13) seems to work, so we can work around it for now.

2
  • Thanks a lot @BlackCap, it works indeed! Very smart!
    – elena
    Commented Sep 29, 2017 at 5:55
  • 17
    I had to use page.keyboard.press(String.fromCharCode(13));
    – vinesh
    Commented Mar 19, 2018 at 12:18
11

await page.keyboard.press('Enter'); This work for me

1
  • This works. Make sure your page is fully loaded first. Commented Apr 14, 2021 at 10:25
6

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();
4

Its page.keyboard.press()

Reference: 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

1

I have used await page.keyboard.press('Enter'); in my code and it worked perfectly fine for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.