【问题标题】:Enter keystroke event not getting dispatched输入击键事件未发送
【发布时间】:2019-03-07 01:57:17
【问题描述】:

尝试通过远程调试协议将输入键击发送到无头 chrome。这是源代码。任何帮助表示赞赏。它似乎不适用于 Chrome 69。除了 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)
  }
}

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      相关资源
      最近更新 更多