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 #226

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

Sending 'Enter' key event on input field #226

selfrefactor opened this issue Aug 4, 2017 · 4 comments

Comments

@selfrefactor
Copy link

selfrefactor commented Aug 4, 2017

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

First I want to thank you for the wonderful library and your efforts in closing the issues.

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!

@cyrus-and
Copy link
Owner

Thanks! :)

You're right, that doesn't work and the documentation isn't really helpful. I even tried several other combinations with no luck. You should ping the guys on the Google Group or file an issue to the official protocol repo.

I don't know your use case but in the meantime you can achieve a similar effect with something like:

Runtime.evaluate({
   expression: `document.querySelector('#lst-ib').value = 'moe';
                document.querySelector('#tsf').submit();`
});

@selfrefactor
Copy link
Author

Thank you the fastest reply :)

I submitted issues in the both links you refer in your answer:

Your workaround code gives me another option and it is nice to have it.

Feel free to close the issue, as it seems there is nothing more we can do for now.

@ckpunmkug
Copy link

ckpunmkug commented Apr 29, 2019

Working example

Press Enter after type in google search input

const CDP = require('chrome-remote-interface');

async function connect() {

  let client;

  try {
    client = await CDP();
    const {Input} = client;
    await Input.setIgnoreInputEvents({ignore: false});

    await Input.insertText({text: "tits"});

    Input.dispatchKeyEvent({"type" : "keyDown", key: "Enter"}).then(() => {
      setTimeout(() => {
        Input.dispatchKeyEvent({"type" : "char", key: "Enter", unmodifiedText: "\r", text: "\r"}).then(() => {
          setTimeout(() => {
            Input.dispatchKeyEvent({"type" : "keyUp", key: "Enter"});
          },50);
        });
      },50);
    });

  } catch (err) {
    console.error(err);
  }
}

connect();

@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
Projects
None yet
Development

No branches or pull requests

4 participants