输入击键事件未调度

时间:2018-10-01 22:45:37

标签: google-chrome google-chrome-devtools selenium-chromedriver chromium remote-debugging

尝试通过远程调试协议将Enter击键发送到无头chrome。这是源代码。任何帮助表示赞赏。它似乎不适用于Chrome69。除了WindowsVirtualKeyCode:13之外,我还尝试了keyIdentifier:'U + 000D',keyIdentifier:'Enter'和key:'\ u000d'。

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)
  }
}

1 个答案:

答案 0 :(得分:0)

See Github Issue 45 , where you also got the above code from. Using the following works:

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