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

nodriver How to send shortcut keys, send_keys(Keys.CONTROL, 'a') #2017

Open
pythonlw opened this issue Sep 12, 2024 · 5 comments
Open

nodriver How to send shortcut keys, send_keys(Keys.CONTROL, 'a') #2017

pythonlw opened this issue Sep 12, 2024 · 5 comments

Comments

@pythonlw
Copy link

How to import the pyppeteer keyboard into nodriver @ultrafunkamsterdam

@HaneulNakamoto
Copy link

waiting

@boludoz
Copy link

boludoz commented Sep 12, 2024

You should read the CDP documentation and use low-level functions.

https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-dispatchKeyEvent

    async def send_keys(self, text: str):
        """
        send text to an input field, or any other html element.

        hint, if you ever get stuck where using py:meth:`~click`
        does not work, sending the keystroke \\n or \\r\\n or a spacebar work wonders!

        :param text: text to send
        :return: None
        """
        await self.apply("(elem) => elem.focus()")
        [
            await self._tab.send(cdp.input_.dispatch_key_event("char", text=char))
            for char in list(text)
        ]


@boludoz
Copy link

boludoz commented Sep 13, 2024

Port here #2019

@boludoz
Copy link

boludoz commented Sep 13, 2024

nodriver.zip

import asyncio
import nodriver as uc

async def main():
    browser = await uc.start()
    tab_instance = await browser.get('https://www.google.com/')
    await asyncio.sleep(1)
    with tab_instance.keyboard as keyboard_instance:
        await (await tab_instance.find('//input[1]')).click()
        await keyboard_instance.type('Hello World!')
        await keyboard_instance.down('Enter')
        await asyncio.sleep(5)

if __name__ == '__main__':

    # since asyncio.run never worked (for me)
    uc.loop().run_until_complete(main())

Based on:
https://github.com/pyppeteer/pyppeteer/blob/dev/pyppeteer/input.py
Mattwmaster58

Mission accomplished, soldier! 🫡

@kieuhuyhoang
Copy link

here is how i handle it, add it right below the send_keys function:

async def send_crl_key(self, text: str):
await self.apply("(elem) => elem.focus()")
[
await self.tab.send(cdp.input.dispatch_key_event(
type_='rawKeyDown',
windows_virtual_key_code=ord(text.upper()),
modifiers=2
)),
await self.tab.send(cdp.input.dispatch_key_event(
type_='keyUp',
windows_virtual_key_code=ord(text.upper()),
modifiers=2
))
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants