输入击键事件未被分派

Enter keystroke event not getting dispatched

正在尝试通过远程调试协议将 enter 击键发送到 headless chrome。这是源代码。任何帮助表示赞赏。它似乎不适用于 Chrome 69。我还尝试了 keyIdentifier: 'U+000D', keyIdentifier: 'Enter' 和 key: '\u000d' 除了 windowsVirtualKeyCode: 13.

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: 't' })
    await R.delay(200)
    await Input.dispatchKeyEvent({ type: 'char', text: 'e' })
    await R.delay(200)
    await Input.dispatchKeyEvent({ type: 'char', text: 's' })
    await R.delay(200)
    await Input.dispatchKeyEvent({ type: 'char', text: 't' })
    await R.delay(200)

    await Input.dispatchKeyEvent({ type: 'rawKeyDown', windowsVirtualKeyCode: 13 })
    await R.delay(3000)
  }catch(err){
    console.log(err)
  }
}

参见 Github Issue 45 ,您也从中获得了上述代码。使用以下作品:

await Input.dispatchKeyEvent({ type: 'char', text: "\r" })