Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending 'Enter' key event on input field #45

Closed
selfrefactor opened this issue Aug 4, 2017 · 6 comments
Closed

Sending 'Enter' key event on input field #45

selfrefactor opened this issue Aug 4, 2017 · 6 comments

Comments

@selfrefactor
Copy link

The issue was originally submitted to 'chrome-remote-interface' repo at cyrus-and/chrome-remote-interface#226 and I was redirected to this repo.


Component Version
Operating system Ubuntu 16.10
Node.js 8.1.2
Chrome/Chromium/... 60.0.3112.78 (Official Build) beta (64-bit)
chrome-remote-interface 0.24.3

Is Chrome running in a container? NO


My issue is with firing 'Enter' key on input field.

I thought that the code await Input.dispatchKeyEvent({ type: 'rawKeyDown', keyIdentifier: 'Enter' }) would work, but it did not.

This is my code:

const CDP = require("chrome-remote-interface")
const chromeLauncher = require("chrome-launcher")
const getPort = require("get-port")
const R = require("rambdax")

const chromeFlags = [
  "--disable-gpu",
  "--disable-sync",
  "--no-first-run",
  "--headless",
  "--window-size=1366,768"
]

const main = async () => {
  try{
    const port = await getPort()
    const chrome = await chromeLauncher.launch({
      chromeFlags,
      port,
    })
    const client = await CDP({ port })
    const { Page, Runtime, Input } = client

    await Promise.all([
      Page.enable(),
      Runtime.enable(),
    ])

    await Page.navigate({ url : 'https://www.google.com' })
    await Page.loadEventFired()
    await R.delay(1000)
    await Input.dispatchKeyEvent({ type: 'char', text: 'm' })
    await R.delay(200)
    await Input.dispatchKeyEvent({ type: 'char', text: 'o' })
    await R.delay(200)
    await Input.dispatchKeyEvent({ type: 'char', text: 'e' })
    await R.delay(200)
    await Input.dispatchKeyEvent({ type: 'rawKeyDown', keyIdentifier: 'Enter' })
    await R.delay(3000)
  }catch(err){
    console.log(err)
  }
}

main()

Any help will be appreciated. Thanks!

@kdzwinel
Copy link
Contributor

kdzwinel commented Aug 4, 2017

I don't remember the details, but I do remember having the exact same issue. This code worked for me: https://github.com/ChromeDevTools/EmulatedDeviceLab/blob/master/lib/device.js#L192 Try providing windowsVirtualKeyCode: 13 instead of keyIdentifier: 'Enter'.

@kdzwinel
Copy link
Contributor

kdzwinel commented Aug 4, 2017

Also, looking at the protocol docs, you are providing invalid value for keyIdentifier. For enter key you should provide sth like 'U+000D'.

@selfrefactor
Copy link
Author

Thank you so much for the help.

It is a bit late now to test it out, but it looks very promising.

@kensoh
Copy link

kensoh commented Aug 6, 2017

I'm communicating directly to Chrome via websocket (without going through chrome-remote-interface). For me I use Input.dispatchKeyEvent and '\r' to send the enter key event and it works for my use case (simulating enter on input boxes).

@selfrefactor
Copy link
Author

Guys, both suggested solutions by @kdzwinel and @kensoh are working, thank you so much.

Feel free to close the issue.

@kdzwinel kdzwinel closed this as completed Aug 7, 2017
@activeliang
Copy link

async pressedEnter() {
  await Input.dispatchKeyEvent({ "type": "rawKeyDown", "windowsVirtualKeyCode": 13, "unmodifiedText": "\r", "text": "\r" })
  await Input.dispatchKeyEvent({ "type": "char", "windowsVirtualKeyCode": 13, "unmodifiedText": "\r", "text": "\r" })
  await Input.dispatchKeyEvent({ "type": "keyUp", "windowsVirtualKeyCode": 13, "unmodifiedText": "\r", "text": "\r" })
}

it works for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants