ultrafunkamsterdam / undetected-chromedriver

Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)
https://github.com/UltrafunkAmsterdam/undetected-chromedriver
GNU General Public License v3.0
10.1k stars 1.17k forks source link

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

Open pythonlw opened 2 months ago

pythonlw commented 2 months ago

How to import the pyppeteer keyboard into nodriver @ultrafunkamsterdam

HaneulNakamoto commented 2 months ago

waiting

boludoz commented 2 months ago

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 commented 2 months ago

Port here #2019

boludoz commented 2 months ago

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 commented 2 months ago

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_keyevent( type='rawKeyDown', windows_virtual_key_code=ord(text.upper()), modifiers=2 )), await self.tab.send(cdp.input.dispatch_keyevent( type='keyUp', windows_virtual_key_code=ord(text.upper()), modifiers=2 )) ]