chrome cdp Input.dispatchKeyEventchrome

由於 nodriver 暫時還無法送出 Enter,解法:

await tab.send(cdp.input_.dispatch_key_event("keyDown", code="Enter", key="Enter", text="\r", windows_virtual_key_code=13))
await tab.send(cdp.input_.dispatch_key_event("keyUp", code="Enter", key="Enter", text="\r", windows_virtual_key_code=13))

nodriver 程式碼:
https://github.com/ultrafunkamsterdam/nodriver/blob/main/nodriver/cdp/input_.py

DrissionPage 程式碼:
DrissionPage/DrissionPage/_functions/keys.py
https://github.com/g1879/DrissionPage/blob/master/DrissionPage/_functions/keys.py

def send_key(page, modifier, key):
    """发送一个字,在键盘中的字符触发按键,其它直接发送文本"""
    if key not in keyDefinitions:
        page.run_cdp('Input.insertText', text=key, _ignore=AlertExistsError)

    else:
        description = keyDescriptionForString(modifier, key)
        text = description['text']
        data = {'type': 'keyDown' if text else 'rawKeyDown',
                'modifiers': modifier,
                'windowsVirtualKeyCode': description['keyCode'],
                'code': description['code'],
                'key': description['key'],
                'text': text,
                'autoRepeat': False,
                'unmodifiedText': text,
                'location': description['location'],
                'isKeypad': description['location'] == 3,
                '_ignore': AlertExistsError}

        page.run_cdp('Input.dispatchKeyEvent', **data)
        data['type'] = 'keyUp'
        page.run_cdp('Input.dispatchKeyEvent', **data)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *